Comparing version 0.1.0 to 0.2.0
@@ -1,1 +0,1 @@ | ||
(function(root,factory){if(typeof exports==="object"&&typeof module==="object")module.exports=factory();else if(typeof define==="function"&&define.amd)define("EventBus",[],factory);else if(typeof exports==="object")exports["EventBus"]=factory();else root["EventBus"]=factory()})(this,function(){var EventBusClass={};EventBusClass=function(){this.listeners={}};EventBusClass.prototype={addEventListener:function(type,callback,scope){var args=[];var numOfArgs=arguments.length;for(var i=0;i<numOfArgs;i++){args.push(arguments[i])}args=args.length>3?args.splice(3,args.length-1):[];if(typeof this.listeners[type]!="undefined"){this.listeners[type].push({scope:scope,callback:callback,args:args})}else{this.listeners[type]=[{scope:scope,callback:callback,args:args}]}},removeEventListener:function(type,callback,scope){if(typeof this.listeners[type]!="undefined"){var numOfCallbacks=this.listeners[type].length;var newArray=[];for(var i=0;i<numOfCallbacks;i++){var listener=this.listeners[type][i];if(listener.scope==scope&&listener.callback==callback){}else{newArray.push(listener)}}this.listeners[type]=newArray}},hasEventListener:function(type,callback,scope){if(typeof this.listeners[type]!="undefined"){var numOfCallbacks=this.listeners[type].length;if(callback===undefined&&scope===undefined){return numOfCallbacks>0}for(var i=0;i<numOfCallbacks;i++){var listener=this.listeners[type][i];if((scope?listener.scope==scope:true)&&listener.callback==callback){return true}}}return false},dispatch:function(type,target){var numOfListeners=0;var event={type:type,target:target};var args=[];var numOfArgs=arguments.length;for(var i=0;i<numOfArgs;i++){args.push(arguments[i])}args=args.length>2?args.splice(2,args.length-1):[];args=[event].concat(args);if(typeof this.listeners[type]!="undefined"){var numOfCallbacks=this.listeners[type].length;for(var i=0;i<numOfCallbacks;i++){var listener=this.listeners[type][i];if(listener&&listener.callback){var concatArgs=args.concat(listener.args);listener.callback.apply(listener.scope,concatArgs);numOfListeners+=1}}}},getEvents:function(){var str="";for(var type in this.listeners){var numOfCallbacks=this.listeners[type].length;for(var i=0;i<numOfCallbacks;i++){var listener=this.listeners[type][i];str+=listener.scope&&listener.scope.className?listener.scope.className:"anonymous";str+=" listen for '"+type+"'\n"}}return str}};var EventBus=new EventBusClass;return EventBus}); | ||
(function(root,factory){if(typeof exports==="object"&&typeof module==="object")module.exports=factory();else if(typeof define==="function"&&define.amd)define("EventBus",[],factory);else if(typeof exports==="object")exports["EventBus"]=factory();else root["EventBus"]=factory()})(this,function(){var EventBusClass={};EventBusClass=function(){this.listeners={}};EventBusClass.prototype={addEventListener:function(type,callback,scope){var args=[];var numOfArgs=arguments.length;for(var i=0;i<numOfArgs;i++){args.push(arguments[i])}args=args.length>3?args.splice(3,args.length-1):[];if(typeof this.listeners[type]!="undefined"){this.listeners[type].push({scope:scope,callback:callback,args:args})}else{this.listeners[type]=[{scope:scope,callback:callback,args:args}]}},removeEventListener:function(type,callback,scope){if(typeof this.listeners[type]!="undefined"){var numOfCallbacks=this.listeners[type].length;var newArray=[];for(var i=0;i<numOfCallbacks;i++){var listener=this.listeners[type][i];if(listener.scope==scope&&listener.callback==callback){}else{newArray.push(listener)}}this.listeners[type]=newArray}},hasEventListener:function(type,callback,scope){if(typeof this.listeners[type]!="undefined"){var numOfCallbacks=this.listeners[type].length;if(callback===undefined&&scope===undefined){return numOfCallbacks>0}for(var i=0;i<numOfCallbacks;i++){var listener=this.listeners[type][i];if((scope?listener.scope==scope:true)&&listener.callback==callback){return true}}}return false},dispatch:function(type,target){var event={type:type,target:target};var args=[];var numOfArgs=arguments.length;for(var i=0;i<numOfArgs;i++){args.push(arguments[i])}args=args.length>2?args.splice(2,args.length-1):[];args=[event].concat(args);if(typeof this.listeners[type]!="undefined"){var listeners=this.listeners[type].slice();var numOfCallbacks=listeners.length;for(var i=0;i<numOfCallbacks;i++){var listener=listeners[i];if(listener&&listener.callback){var concatArgs=args.concat(listener.args);listener.callback.apply(listener.scope,concatArgs)}}}},getEvents:function(){var str="";for(var type in this.listeners){var numOfCallbacks=this.listeners[type].length;for(var i=0;i<numOfCallbacks;i++){var listener=this.listeners[type][i];str+=listener.scope&&listener.scope.className?listener.scope.className:"anonymous";str+=" listen for '"+type+"'\n"}}return str}};var EventBus=new EventBusClass;return EventBus}); |
{ | ||
"name": "eventbusjs", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Managing events with JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "lib/eventbus.min.js", |
@@ -66,3 +66,3 @@ # Simple JavaScript class for managing events in JavaScript | ||
``` | ||
## Usage | ||
@@ -125,1 +125,21 @@ | ||
## Example of usage EventBus.removeEventListener | ||
To remove EventListener you have to pass same instance of callback | ||
```js | ||
/* Wrong - callback functions are different instances */ | ||
EventBus.addEventListener('EXAMPLE_EVENT', function() { | ||
console.log('example callback'); | ||
}); | ||
EventBus.removeEventListener('EXAMPLE_EVENT', function() { | ||
console.log('example callback'); | ||
}); | ||
/* Correct - callback function is the same instance */ | ||
var handler = function() { | ||
console.log('example callback'); | ||
}; | ||
EventBus.addEventListener('EXAMPLE_EVENT', handler); | ||
EventBus.removeEventListener('EXAMPLE_EVENT', handler); | ||
``` |
@@ -17,3 +17,3 @@ (function (root, factory) { | ||
EventBusClass.prototype = { | ||
addEventListener:function(type, callback, scope) { | ||
addEventListener: function(type, callback, scope) { | ||
var args = []; | ||
@@ -23,3 +23,3 @@ var numOfArgs = arguments.length; | ||
args.push(arguments[i]); | ||
} | ||
} | ||
args = args.length > 3 ? args.splice(3, args.length-1) : []; | ||
@@ -32,3 +32,3 @@ if(typeof this.listeners[type] != "undefined") { | ||
}, | ||
removeEventListener:function(type, callback, scope) { | ||
removeEventListener: function(type, callback, scope) { | ||
if(typeof this.listeners[type] != "undefined") { | ||
@@ -40,3 +40,3 @@ var numOfCallbacks = this.listeners[type].length; | ||
if(listener.scope == scope && listener.callback == callback) { | ||
} else { | ||
@@ -49,3 +49,3 @@ newArray.push(listener); | ||
}, | ||
hasEventListener:function(type, callback, scope) { | ||
hasEventListener: function(type, callback, scope) { | ||
if(typeof this.listeners[type] != "undefined") { | ||
@@ -65,7 +65,6 @@ var numOfCallbacks = this.listeners[type].length; | ||
}, | ||
dispatch:function(type, target) { | ||
var numOfListeners = 0; | ||
dispatch: function(type, target) { | ||
var event = { | ||
type:type, | ||
target:target | ||
type: type, | ||
target: target | ||
}; | ||
@@ -76,13 +75,15 @@ var args = []; | ||
args.push(arguments[i]); | ||
}; | ||
}; | ||
args = args.length > 2 ? args.splice(2, args.length-1) : []; | ||
args = [event].concat(args); | ||
if(typeof this.listeners[type] != "undefined") { | ||
var numOfCallbacks = this.listeners[type].length; | ||
var listeners = this.listeners[type].slice(); | ||
var numOfCallbacks = listeners.length; | ||
for(var i=0; i<numOfCallbacks; i++) { | ||
var listener = this.listeners[type][i]; | ||
if(listener && listener.callback) { | ||
var listener = listeners[i]; | ||
if(listener && listener.callback) { | ||
var concatArgs = args.concat(listener.args); | ||
listener.callback.apply(listener.scope, concatArgs); | ||
numOfListeners += 1; | ||
} | ||
@@ -92,3 +93,3 @@ } | ||
}, | ||
getEvents:function() { | ||
getEvents: function() { | ||
var str = ""; | ||
@@ -108,2 +109,2 @@ for(var type in this.listeners) { | ||
return EventBus; | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
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
13397
144