Comparing version 0.1.1 to 0.2.0
{ | ||
"name": "uevents.js", | ||
"main": "uevents.js", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/acornejo/uevents.js", | ||
@@ -6,0 +6,0 @@ "authors": [ |
{ | ||
"name": "uevents.js", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"author": "Alex Cornejo", | ||
@@ -5,0 +5,0 @@ "main": "uevents.js", |
@@ -16,3 +16,3 @@ /*jshint expr:true */ | ||
beforeEach(function () { | ||
ctx.obj = new uevents(); | ||
ctx.obj = uevents.create(); | ||
ctx.signal1_listener1 = new sinon.spy(); | ||
@@ -92,3 +92,3 @@ ctx.signal1_listener2 = new sinon.spy(); | ||
ctx.obj = new Date(); | ||
uevents.extend(ctx.obj); | ||
uevents.create(ctx.obj); | ||
ctx.signal1_listener1 = new sinon.spy(); | ||
@@ -95,0 +95,0 @@ ctx.signal1_listener2 = new sinon.spy(); |
169
uevents.js
(function () { | ||
var uevents = { | ||
/** | ||
* Create new events object. | ||
* | ||
* **Example:** | ||
* | ||
* var obj = new uevents(); | ||
* | ||
* <br> | ||
* Using new is optional but recommended. | ||
* | ||
*/ | ||
/** | ||
* Create a new uevents object or extend | ||
* an existing object with the uevents api. | ||
* | ||
* **Example:** | ||
* | ||
* var obj = uevents.create(); | ||
* | ||
* // to extend existing object use | ||
* | ||
* var other_obj = {}; | ||
* uevents.create(other_obj); | ||
*/ | ||
function uevents() { | ||
if (!(this instanceof uevents)) | ||
return new uevents(); | ||
create: function(obj) { | ||
if (typeof obj === 'undefined') { | ||
var self = Object.create(this); | ||
self._events = {}; | ||
return self; | ||
} | ||
else { | ||
var data = {_events: {}}; | ||
obj.on = uevents.on.bind(data); | ||
obj.once = uevents.once.bind(data); | ||
obj.off = uevents.off.bind(data); | ||
obj.trigger = uevents.trigger.bind(data); | ||
return obj; | ||
} | ||
}, | ||
this._events = {}; | ||
} | ||
/** | ||
@@ -34,3 +46,3 @@ * Registers a listener for a specified event. | ||
* | ||
* var obj = new uevents(), ctx = {}; | ||
* var obj = uevents.create(), ctx = {name: 'context'}; | ||
* obj.on('signal', function (param) { | ||
@@ -48,9 +60,9 @@ * console.log('signal called with parameter ' + param + ' and context ' + this); | ||
*/ | ||
uevents.prototype.on = function (name, callback, context) { | ||
if (!this._events.hasOwnProperty(name)) | ||
this._events[name] = []; | ||
this._events[name].push([callback, context]); | ||
on: function (name, callback, context) { | ||
if (!this._events.hasOwnProperty(name)) | ||
this._events[name] = []; | ||
this._events[name].push([callback, context]); | ||
return this; | ||
}; | ||
return this; | ||
}, | ||
@@ -69,3 +81,3 @@ /** | ||
* | ||
* var obj = new uevents(); | ||
* var obj = uevents.create(); | ||
* obj.once('signal', function() { | ||
@@ -79,11 +91,10 @@ * console.log('signal called (this will only be printed once)'); | ||
*/ | ||
uevents.prototype.once = function (name, callback, context) { | ||
var self = this, once = function () { | ||
self.off(name, once); | ||
callback.apply(context || this, arguments); | ||
}; | ||
return this.on(name, once, context); | ||
}; | ||
once: function (name, callback, context) { | ||
var self = this, once = function () { | ||
self.off(name, once); | ||
callback.apply(context || this, arguments); | ||
}; | ||
return this.on(name, once, context); | ||
}, | ||
/** | ||
@@ -115,23 +126,23 @@ * Removes listener(s) from an event. | ||
*/ | ||
uevents.prototype.off = function (name, callback, context) { | ||
if (!name) { | ||
this._events = {}; | ||
} else if (this._events.hasOwnProperty(name)) { | ||
if (!callback && !context) { | ||
delete this._events[name]; | ||
} else { | ||
var event = this._events[name], filtered = []; | ||
for (var i = 0, len = event.length; i < len; i++) { | ||
if ((!callback || event[i][0] !== callback) && (!context || event[i][1] === context)) | ||
filtered.push(event[i]); | ||
off: function (name, callback, context) { | ||
if (!name) { | ||
this._events = {}; | ||
} else if (this._events.hasOwnProperty(name)) { | ||
if (!callback && !context) { | ||
delete this._events[name]; | ||
} else { | ||
var event = this._events[name], filtered = []; | ||
for (var i = 0, len = event.length; i < len; i++) { | ||
if ((!callback || event[i][0] !== callback) && (!context || event[i][1] === context)) | ||
filtered.push(event[i]); | ||
} | ||
if (!filtered.length) | ||
delete this._events[name]; | ||
else | ||
this._events[name] = filtered; | ||
} | ||
if (!filtered.length) | ||
delete this._events[name]; | ||
else | ||
this._events[name] = filtered; | ||
} | ||
} | ||
return this; | ||
}; | ||
return this; | ||
}, | ||
@@ -153,50 +164,18 @@ /** | ||
*/ | ||
uevents.prototype.trigger = function (name) { | ||
if (this._events.hasOwnProperty(name)) { | ||
var args = Array.prototype.slice.call(arguments, 1), | ||
callbacks = this._events[name], | ||
len = callbacks.length; | ||
for (var i = 0; i < len; i++) { | ||
var callback = callbacks[i][0], context = callbacks[i][1]; | ||
callback.apply(context || this, args); | ||
trigger: function (name) { | ||
if (this._events.hasOwnProperty(name)) { | ||
var args = Array.prototype.slice.call(arguments, 1), | ||
callbacks = this._events[name], | ||
len = callbacks.length; | ||
for (var i = 0; i < len; i++) { | ||
var callback = callbacks[i][0], context = callbacks[i][1]; | ||
callback.apply(context || this, args); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
/** | ||
* Extends and object to handle events. | ||
* | ||
* **Example:** | ||
* | ||
* var obj = {}; | ||
* uevents.extend(obj); | ||
* | ||
* // register listener | ||
* obj.on('signal', function () { | ||
* console.log('signal called'); | ||
* }); | ||
* | ||
* // trigger signal | ||
* obj.trigger('signal'); | ||
* | ||
* // remove listeners | ||
* obj.off('signal'); | ||
* | ||
* <br> | ||
* The same API is available on extend objects than on native uevents | ||
* objects. | ||
* | ||
* @param {Object} obj Object to extend to support event handling. | ||
*/ | ||
uevents.extend = function (obj) { | ||
var data = {_events: {}}; | ||
obj.on = uevents.prototype.on.bind(data); | ||
obj.once = uevents.prototype.once.bind(data); | ||
obj.off = uevents.prototype.off.bind(data); | ||
obj.trigger = uevents.prototype.trigger.bind(data); | ||
}; | ||
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") | ||
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") | ||
module.exports = uevents; | ||
else if (typeof define === "function" && define.amd) | ||
else if (typeof define === "function" && define.amd) | ||
define(["uevents"], function () { return uevents; }); | ||
@@ -203,0 +182,0 @@ else |
@@ -1,1 +0,1 @@ | ||
(function(){function uevents(){if(!(this instanceof uevents))return new uevents;this._events={}}uevents.prototype.on=function(name,callback,context){if(!this._events.hasOwnProperty(name))this._events[name]=[];this._events[name].push([callback,context]);return this};uevents.prototype.once=function(name,callback,context){var self=this,once=function(){self.off(name,once);callback.apply(context||this,arguments)};return this.on(name,once,context)};uevents.prototype.off=function(name,callback,context){if(!name){this._events={}}else if(this._events.hasOwnProperty(name)){if(!callback&&!context){delete this._events[name]}else{var event=this._events[name],filtered=[];for(var i=0,len=event.length;i<len;i++){if((!callback||event[i][0]!==callback)&&(!context||event[i][1]===context))filtered.push(event[i])}if(!filtered.length)delete this._events[name];else this._events[name]=filtered}}return this};uevents.prototype.trigger=function(name){if(this._events.hasOwnProperty(name)){var args=Array.prototype.slice.call(arguments,1),callbacks=this._events[name],len=callbacks.length;for(var i=0;i<len;i++){var callback=callbacks[i][0],context=callbacks[i][1];callback.apply(context||this,args)}}};uevents.extend=function(obj){var data={_events:{}};obj.on=uevents.prototype.on.bind(data);obj.once=uevents.prototype.once.bind(data);obj.off=uevents.prototype.off.bind(data);obj.trigger=uevents.prototype.trigger.bind(data)};if(typeof require==="function"&&typeof exports==="object"&&typeof module==="object")module.exports=uevents;else if(typeof define==="function"&&define.amd)define(["uevents"],function(){return uevents});else window.uevents=uevents})(); | ||
(function(){var uevents={create:function(obj){if(typeof obj==="undefined"){var self=Object.create(this);self._events={};return self}else{var data={_events:{}};obj.on=uevents.on.bind(data);obj.once=uevents.once.bind(data);obj.off=uevents.off.bind(data);obj.trigger=uevents.trigger.bind(data);return obj}},on:function(name,callback,context){if(!this._events.hasOwnProperty(name))this._events[name]=[];this._events[name].push([callback,context]);return this},once:function(name,callback,context){var self=this,once=function(){self.off(name,once);callback.apply(context||this,arguments)};return this.on(name,once,context)},off:function(name,callback,context){if(!name){this._events={}}else if(this._events.hasOwnProperty(name)){if(!callback&&!context){delete this._events[name]}else{var event=this._events[name],filtered=[];for(var i=0,len=event.length;i<len;i++){if((!callback||event[i][0]!==callback)&&(!context||event[i][1]===context))filtered.push(event[i])}if(!filtered.length)delete this._events[name];else this._events[name]=filtered}}return this},trigger:function(name){if(this._events.hasOwnProperty(name)){var args=Array.prototype.slice.call(arguments,1),callbacks=this._events[name],len=callbacks.length;for(var i=0;i<len;i++){var callback=callbacks[i][0],context=callbacks[i][1];callback.apply(context||this,args)}}}};if(typeof require==="function"&&typeof exports==="object"&&typeof module==="object")module.exports=uevents;else if(typeof define==="function"&&define.amd)define(["uevents"],function(){return uevents});else window.uevents=uevents})(); |
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
8
17234
337