eden-class
Advanced tools
Comparing version 0.0.23 to 0.0.24
66
class.js
@@ -14,2 +14,4 @@ module.exports = require('classified-magic')(function() { | ||
this._listeners = []; | ||
/* Private Properties | ||
@@ -100,2 +102,50 @@ -------------------------------*/ | ||
/** | ||
* Unbinds the specified function | ||
* from an event | ||
* | ||
* @param string | ||
* @param function|string | ||
* @return this | ||
*/ | ||
this.off = function(event, callback) { | ||
this.argument() | ||
//argument 1 must be a string | ||
.test(1, 'string') | ||
//argument 2 must be a function or string | ||
.test(2, 'undef', 'function', 'string'); | ||
if(this._events === null) { | ||
return this; | ||
} | ||
if(typeof callback === 'undefined') { | ||
this._events.removeAllListeners(event); | ||
return this; | ||
} | ||
if(typeof callback === 'string') { | ||
for(var i = 0; i < this._listeners.length; i++) { | ||
if(this._listeners[i].original.name !== callback) { | ||
continue; | ||
} | ||
this._events.removeListener(event, this._listeners[i].callback); | ||
} | ||
return this; | ||
} | ||
//it is a function | ||
for(var i = 0; i < this._listeners.length; i++) { | ||
if(this._listeners[i].original !== callback) { | ||
continue; | ||
} | ||
this._events.removeListener(event, this._listeners[i].callback); | ||
} | ||
return this; | ||
}; | ||
/** | ||
* Attaches an instance to be notified | ||
@@ -120,4 +170,10 @@ * when an event has been triggered | ||
this._events.on(event, callback.bind(this)); | ||
var binded = callback.bind(this); | ||
this._listeners.push({ | ||
original: callback, | ||
callback: binded }); | ||
this._events.on(event, binded); | ||
return this; | ||
@@ -147,3 +203,9 @@ }; | ||
this._events.once(event, callback.bind(this)); | ||
var binded = callback.bind(this); | ||
this._listeners.push({ | ||
original: callback, | ||
callback: binded }); | ||
this._events.once(event, binded); | ||
@@ -150,0 +212,0 @@ return this; |
{ | ||
"name": "eden-class", | ||
"description": "Eden JS Base Class", | ||
"version": "0.0.23", | ||
"version": "0.0.24", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Christian Blanquera", |
@@ -79,3 +79,58 @@ var assert = require('assert'); | ||
}); | ||
it('Should bind and unbind events', function() { | ||
var stack = [], cb = function(data) { | ||
stack.push('g-cb'); | ||
assert.equal(4, data); | ||
}; | ||
function cb2(data) { | ||
stack.push('g-cb2'); | ||
assert.equal(4, data); | ||
} | ||
user.on('foo', function(data) { | ||
stack.push('g-anon'); | ||
assert.equal(4, data); | ||
}) | ||
.on('foo', cb) | ||
.on('foo', cb2); | ||
(function() { | ||
var cb = function(data) { | ||
stack.push('cb'); | ||
assert.equal(4, data); | ||
}; | ||
user.on('foo', cb); | ||
})(); | ||
user.trigger('foo', 4); | ||
assert.equal('g-anon', stack[0]); | ||
assert.equal('g-cb', stack[1]); | ||
assert.equal('g-cb2', stack[2]); | ||
assert.equal('cb', stack[3]); | ||
stack = []; | ||
user.off('foo', cb2).trigger('foo', 4); | ||
assert.equal('g-anon', stack[0]); | ||
assert.equal('g-cb', stack[1]); | ||
assert.equal('cb', stack[2]); | ||
stack = []; | ||
user.off('foo', 'cb').trigger('foo', 4); | ||
assert.equal('g-anon', stack[0]); | ||
stack = []; | ||
user.off('foo').trigger('foo', 4); | ||
assert.equal(0, stack.length); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20786
550