node-bound
Advanced tools
Comparing version 0.2.2 to 1.0.0
21
index.js
@@ -10,4 +10,3 @@ /* jshint browser: true */ | ||
// here to store refs to bound functions | ||
var _fns = {}, | ||
md5 = require( 'blueimp-md5' ).md5, | ||
var md5 = require( 'blueimp-md5' ).md5, | ||
utils = require( './src/utils' ); | ||
@@ -20,3 +19,4 @@ | ||
} | ||
var _id = md5( handler.toString() + JSON.stringify( utils.decycleObject( context ) ) ); | ||
var _id = md5( handler.toString() + JSON.stringify( utils.decycleObject( context.slice( 1 ) ) ) ), | ||
_context = context[ 0 ]; | ||
/* | ||
@@ -26,9 +26,12 @@ this is to cache the function so it can be unbound from the event | ||
*/ | ||
if ( !_fns[ eventName ] ) { | ||
_fns[eventName] = {}; | ||
_context._boundListeners = _context._boundListeners || {}; | ||
if ( !_context._boundListeners[ eventName ] ) { | ||
_context._boundListeners[ eventName ] = {}; | ||
} | ||
if ( !_fns[ eventName ][ _id ] ) { | ||
_fns[ eventName ][ _id ] = handler.bind.apply( handler, context ); | ||
if ( !_context._boundListeners[ eventName ][ _id ] ) { | ||
_context._boundListeners[ eventName ][ _id ] = handler.bind.apply( handler, context ); | ||
} | ||
handler = _fns[ eventName ][ _id ]; | ||
handler = _context._boundListeners[ eventName ][ _id ]; | ||
@@ -38,3 +41,3 @@ fn( eventName, handler ); | ||
if ( removeCache ) { | ||
delete _fns[ eventName ][ _id ]; | ||
delete _context._boundListeners[ eventName ][ _id ]; | ||
} | ||
@@ -41,0 +44,0 @@ }; |
{ | ||
"name": "node-bound", | ||
"version": "0.2.2", | ||
"version": "1.0.0", | ||
"description": "A simple module to help streamline event binding", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -189,19 +189,24 @@ var assert = require('assert'), | ||
describe( '#unbind', function ( ) { | ||
it('should unbind an event to an emitter', function ( done ) { | ||
function handle ( msg ) { | ||
throw new Error('Error event handle executed when it should not have'); | ||
} | ||
setTimeout( done, 500 ); | ||
bound( emitter, { | ||
'test7' : handle | ||
}); | ||
bound.unbind( emitter, { | ||
'test7' : handle | ||
}); | ||
emitter.emit('test7', 'hello world'); | ||
it('should unbind an event to an emitter', function ( done ) { | ||
var context = { | ||
id: '1' | ||
}; | ||
function handle ( msg ) { | ||
throw new Error('Error event handle executed when it should not have'); | ||
} | ||
setTimeout( done, 500 ); | ||
bound( emitter, { | ||
'test7' : handle | ||
}, context ); | ||
bound.unbind( emitter, { | ||
'test7' : handle | ||
}, context ); | ||
emitter.emit('test7', 'hello world'); | ||
}); | ||
}); | ||
describe( '#unbind', function ( ) { | ||
describe( '#bind', function ( ) { | ||
it('should bind an event to an object given if a proper handler is given', function( done ){ | ||
var context = {}; | ||
function handle ( msg ) { | ||
@@ -212,8 +217,10 @@ assert.equal( 'hello world', msg ); | ||
} | ||
var context = { | ||
handle : handle, | ||
nice : 'ice' | ||
}; | ||
bound.bind( emitter, { | ||
'test8' : 'handle' | ||
}, { | ||
handle : handle, | ||
nice : 'ice' | ||
}); | ||
}, context ); | ||
assert.equal( typeof context._boundListeners, 'object' ) | ||
emitter.emit('test8', 'hello world'); | ||
@@ -220,0 +227,0 @@ }); |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
18378
386
1