Comparing version 1.0.0 to 1.0.1
@@ -114,3 +114,7 @@ module.exports = chainit; | ||
chainit.add = function add(to, fnName, fn) { | ||
to.__addToChain(fnName, fn); | ||
if (to.prototype && to.prototype.__addToChain) { | ||
to.prototype.__addToChain(fnName, fn); | ||
} else { | ||
to.__addToChain(fnName, fn); | ||
} | ||
} | ||
@@ -117,0 +121,0 @@ |
{ | ||
"name": "chainit", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Turn an asynchronous JavaScript api into an asynchronous chainable JavaScript api.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -44,11 +44,10 @@ [![Build Status](https://travis-ci.org/vvo/chainit.png)](https://travis-ci.org/vvo/chainit) | ||
var MyChainApi = chainit(MyApi); | ||
var original1 = MyChainApi.prototype.method1; | ||
var obj = new MyChainApi(); | ||
// override instance method | ||
chainit.add(MyChainApi, 'method1', function(cb) { | ||
chainit.add(obj, 'method1', function(cb) { | ||
cb() | ||
}); | ||
var obj = new MyChainApi(); | ||
obj | ||
@@ -59,8 +58,12 @@ .method1() // calls the newly added method1 | ||
// revert original method | ||
chainit.add(MyChainApi, 'method1', original1); | ||
chainit.add(MyChainApi, 'method1', MyApi.prototype.method1); | ||
// override prototype method | ||
chainit.add(MyChainApi.prototype, 'method1', function(cb) { | ||
chainit.add(MyChainApi, 'method1', function(cb) { | ||
cb() | ||
}); | ||
var obj2 = new MyChainApi(); | ||
obj2.method1(); // calls the newly chained prototype `method1` | ||
``` | ||
@@ -67,0 +70,0 @@ |
@@ -218,3 +218,3 @@ describe('chaining an Api', function() { | ||
chainit.add(ChainApi.prototype, 'concat', function concat(sub, cb) { | ||
chainit.add(ChainApi, 'concat', function concat(sub, cb) { | ||
this.s = this.s.concat(sub + '#'); | ||
@@ -232,5 +232,5 @@ setTimeout(cb, ChainApi.getRandomArbitrary(5, 20)); | ||
assert.equal(this.s, 're#de#fi1-#fi2-#fi3-#nition#'); | ||
chainit.add(ChainApi.prototype, 'concat', original); | ||
chainit.add(ChainApi, 'concat', original); | ||
o.concat('BOUH-', function() { | ||
chainit.add(ChainApi.prototype, 'concat', function concat(sub, cb) { | ||
chainit.add(ChainApi, 'concat', function concat(sub, cb) { | ||
this.s = this.s.concat(sub + '!!'); | ||
@@ -240,3 +240,3 @@ setTimeout(cb, ChainApi.getRandomArbitrary(5, 20)); | ||
o.concat('hello', function() { | ||
chainit.add(ChainApi.prototype, 'concat', Api.prototype.concat); | ||
chainit.add(ChainApi, 'concat', Api.prototype.concat); | ||
o.concat('-ah', function() { | ||
@@ -243,0 +243,0 @@ o2.concat('def', function() { |
Sorry, the diff of this file is not supported yet
18379
447
126