Comparing version 1.1.0 to 2.0.0
module.exports = function (grunt) { | ||
require("load-grunt-tasks")(grunt); | ||
// Project configuration. | ||
@@ -36,9 +38,10 @@ grunt.initConfig({ | ||
} | ||
}, | ||
release: { | ||
options: { | ||
npm: true | ||
} | ||
} | ||
}); | ||
grunt.loadNpmTasks("grunt-contrib-watch"); | ||
grunt.loadNpmTasks("grunt-contrib-concat"); | ||
grunt.loadNpmTasks("grunt-contrib-uglify"); | ||
grunt.loadNpmTasks("grunt-contrib-jshint"); | ||
@@ -51,2 +54,2 @@ // Default task. | ||
}; | ||
}; |
@@ -47,64 +47,20 @@ // Eventify | ||
// Returns a partial implementation matching the minimal API subset required | ||
// by Backbone.Events | ||
function miniscore() { | ||
return { | ||
// Retrieve the names of an object's properties. | ||
// Delegates to **ECMAScript 5**'s native `Object.keys` | ||
keys: Object.keys || function (obj) { | ||
if (typeof obj !== "object" && typeof obj !== "function" || obj === null) { | ||
throw new TypeError("keys() called on a non-object"); | ||
} | ||
var key, keys = []; | ||
for (key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
keys[keys.length] = key; | ||
} | ||
} | ||
return keys; | ||
}, | ||
uniqueId: function (prefix) { | ||
idCounter = idCounter + 1; | ||
var id = idCounter + ''; | ||
return prefix ? prefix + id : id; | ||
}, | ||
function uniqueId(prefix) { | ||
idCounter = idCounter + 1; | ||
var id = idCounter + ''; | ||
return prefix ? prefix + id : id; | ||
} | ||
each: function (obj, iterator, context) { | ||
var key, i, l; | ||
if (obj == null) { | ||
return; | ||
} | ||
if (nativeForEach && obj.forEach === nativeForEach) { | ||
obj.forEach(iterator, context); | ||
} else if (obj.length === +obj.length) { | ||
for (i = 0, l = obj.length; i < l; i += 1) { | ||
if (iterator.call(context, obj[i], i, obj) === breaker) { | ||
return; | ||
} | ||
} | ||
} else { | ||
for (key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
if (iterator.call(context, obj[key], key, obj) === breaker) { | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
once: function (func) { | ||
var ran = false, | ||
memo; | ||
return function () { | ||
if (ran) { | ||
return memo; | ||
} | ||
ran = true; | ||
memo = func.apply(this, arguments); | ||
func = null; | ||
return memo; | ||
}; | ||
function once(func) { | ||
var ran = false, | ||
memo; | ||
return function () { | ||
if (ran) { | ||
return memo; | ||
} | ||
ran = true; | ||
memo = func.apply(this, arguments); | ||
func = null; | ||
return memo; | ||
}; | ||
@@ -114,3 +70,2 @@ } | ||
var EventifyInstance, | ||
triggerEvents, | ||
listenMethods = { | ||
@@ -120,10 +75,5 @@ listenTo: 'on', | ||
}, | ||
breaker = {}, | ||
nativeForEach = Array.prototype.forEach, | ||
slice = Array.prototype.slice, | ||
idCounter = 0, | ||
eventsApi, | ||
_ = miniscore(), | ||
// Regular expression used to split event strings | ||
@@ -145,4 +95,2 @@ eventSplitter = /\s+/, | ||
version: "1.1.0", | ||
// Event Functions | ||
@@ -172,3 +120,3 @@ // ----------------- | ||
var self = this, | ||
once; | ||
onceListener; | ||
@@ -179,9 +127,9 @@ if (!eventsApi(this, 'once', name, [callback, context]) || !callback) { | ||
once = _.once(function () { | ||
self.off(name, once); | ||
onceListener = once(function () { | ||
self.off(name, onceListener); | ||
callback.apply(this, arguments); | ||
}); | ||
once._callback = callback; | ||
return this.on(name, once, context); | ||
onceListener._callback = callback; | ||
return this.on(name, onceListener, context); | ||
}, | ||
@@ -203,3 +151,3 @@ | ||
names = name ? [name] : _.keys(this._events); | ||
names = name ? [name] : Object.keys(this._events); | ||
for (i = 0, l = names.length; i < l; i += 1) { | ||
@@ -213,3 +161,5 @@ name = names[i]; | ||
ev = events[j]; | ||
if ((callback && callback !== ev.callback && callback !== ev.callback._callback) || | ||
if ((callback && | ||
callback !== ev.callback && | ||
callback !== ev.callback._callback) || | ||
(context && context !== ev.context)) { | ||
@@ -280,20 +230,2 @@ retain.push(ev); | ||
// Utility Functions | ||
// ----------------- | ||
// Adds the methods on, off and trigger to a target Object | ||
enable: function (target) { | ||
var i, len, | ||
methods = ['on', 'once', 'off', 'trigger', 'stopListening', 'listenTo', | ||
'listenToOnce', 'bind', 'unbind']; | ||
target = target || {}; | ||
for (i = 0, len = methods.length; i < len; i = i + 1) { | ||
target[methods[i]] = this[methods[i]]; | ||
} | ||
return target; | ||
} | ||
}; | ||
@@ -306,3 +238,3 @@ | ||
// in terms of the existing API. | ||
eventsApi = function (obj, action, name, rest) { | ||
function eventsApi(obj, action, name, rest) { | ||
var key, i, l, names; | ||
@@ -334,3 +266,3 @@ | ||
return true; | ||
}; | ||
} | ||
@@ -341,3 +273,3 @@ // A difficult-to-believe, but optimized internal dispatch function for | ||
triggerEvents = function (events, args) { | ||
function triggerEvents(events, args) { | ||
var ev, | ||
@@ -385,3 +317,3 @@ i = 0, | ||
} | ||
}; | ||
} | ||
@@ -392,7 +324,8 @@ | ||
// listening to. | ||
_.each(listenMethods, function (implementation, method) { | ||
Object.keys(listenMethods).forEach(function (method) { | ||
var implementation = listenMethods[method]; | ||
Eventify.prototype[method] = function (obj, name, callback) { | ||
var id, | ||
listeners = this._listeners || (this._listeners = {}); | ||
obj._listenerId = obj._listenerId || _.uniqueId('l'); | ||
obj._listenerId = obj._listenerId || uniqueId('l'); | ||
id = obj._listenerId; | ||
@@ -415,9 +348,35 @@ listeners[id] = obj; | ||
EventifyInstance.version = "2.0.0"; | ||
// Utility Functions | ||
// ----------------- | ||
// Adds the methods on, off and trigger to a target Object | ||
EventifyInstance.enable = function enable(target) { | ||
var i, len, | ||
methods = Object.keys(Eventify.prototype); | ||
target = target || {}; | ||
for (i = 0, len = methods.length; i < len; i = i + 1) { | ||
target[methods[i]] = this[methods[i]]; | ||
} | ||
return target; | ||
}; | ||
EventifyInstance.create = function () { | ||
return Object.create(Eventify.prototype); | ||
}; | ||
// Backbone.Events drop in replacement compatibility | ||
EventifyInstance.mixin = EventifyInstance.enable; | ||
// Expose prototype so other objects can extend it | ||
EventifyInstance.proto = Eventify.prototype; | ||
// Sets Eventify on the browser window or on the process | ||
return EventifyInstance; | ||
// Establish the root object, `window` in the browser, or `global` on the server. | ||
})); | ||
// Establish the root object, `window` in the browser, | ||
// or `global` on the server. | ||
})); |
{ | ||
"name": "eventify", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "Lightweight module that can be mixed in to any object in order to provide it with custom events. For node.js and the browser. Based on Backbone.Events", | ||
@@ -40,10 +40,12 @@ "author": "Bermi Ferrer <bermi@bermilabs.com>", | ||
"blanket": "1.1.6", | ||
"grunt": "0.4.x", | ||
"expect.js": "0.3.1", | ||
"grunt": "0.4.5", | ||
"grunt-cli": "0.1.13", | ||
"grunt-contrib-concat": "0.4.0", | ||
"grunt-contrib-concat": "0.5.0", | ||
"grunt-contrib-jshint": "0.10.0", | ||
"grunt-contrib-uglify": "0.5.0", | ||
"grunt-contrib-uglify": "0.6.0", | ||
"grunt-contrib-watch": "0.6.1", | ||
"mocha": "1.9.0", | ||
"expect.js": "0.2.0", | ||
"grunt-release": "~0.11.0", | ||
"load-grunt-tasks": "~3.1.0", | ||
"mocha": "2.0.1", | ||
"mocha-spec-cov": "0.0.3" | ||
@@ -57,9 +59,9 @@ }, | ||
"iexplore/7..latest", | ||
"firefox/10..latest", | ||
"chrome/20..latest", | ||
"firefox/17..latest", | ||
"chrome/30..latest", | ||
"opera/12..latest", | ||
"safari/4..latest", | ||
"safari/5..latest", | ||
"iphone/6..latest", | ||
"ipad/6..latest", | ||
"android-browser/4.2" | ||
"android-browser/4.2..latest" | ||
], | ||
@@ -66,0 +68,0 @@ "harness": "mocha", |
@@ -9,2 +9,4 @@ # Eventify | ||
*(non-ES5 environments require an ES5 shim)* | ||
## Installing | ||
@@ -45,3 +47,23 @@ | ||
The prototype is also exposed so you can extend it: | ||
function MyEmitter() { | ||
// ... | ||
} | ||
MyEmitter.prototype = Object.create(Eventify.proto); | ||
MyEmitter.prototype.foo = function () { | ||
//... | ||
}; | ||
If you want to create a plain emitter in a lightweight manner, use `Eventify.create()`: | ||
var emitter = Eventify.create(); | ||
emitter.on("foo", function (message) { | ||
//... | ||
}) | ||
### *enable* Eventify.enable(destination) | ||
@@ -104,3 +126,3 @@ | ||
### *once** object.once(event, callback, [context]) | ||
### **once** object.once(event, callback, [context]) | ||
@@ -129,3 +151,3 @@ Just like on, but causes the bound callback to only fire once before being removed. Handy for saying "the next time that X happens, do this". | ||
### *noClonflict* var LocalEventify = Eventify.noConflict(); | ||
### *noConflict* var LocalEventify = Eventify.noConflict(); | ||
@@ -208,2 +230,2 @@ Returns the Eventify object back to its original value. You can use the return value of Eventify.noConflict() to keep a local reference to Eventify. Useful for embedding Eventify on third-party websites, where you don't want to clobber the existing Eventify object. | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
@@ -15,2 +15,6 @@ (function (root) { | ||
var protoMethods = ['on', 'once', 'off', 'trigger', 'stopListening', | ||
'listenTo', 'listenToOnce' | ||
]; | ||
if (!is_commons) { | ||
@@ -31,3 +35,3 @@ describe('No conflict', function () { | ||
Eventify.enable(target); | ||
expect(target).to.include.keys("on,once,off,trigger,stopListening".split(",")); | ||
expect(target).to.include.keys(protoMethods); | ||
}); | ||
@@ -55,9 +59,21 @@ | ||
Eventify.enable(Plop.prototype); | ||
expect(Plop.prototype).to.have.keys(['foo', 'on', 'once', 'off', 'trigger', | ||
'stopListening', 'listenTo', 'listenToOnce', 'bind', 'unbind']); | ||
expect(Plop.prototype).to.only.have.keys(['foo'].concat(protoMethods)); | ||
}); | ||
}); | ||
describe("Eventify.create", function () { | ||
it("should return an empty event emitter", function (done) { | ||
var emitter = Eventify.create(); | ||
protoMethods.forEach(function (method) { | ||
expect(emitter[method]).to.be.a("function"); | ||
}); | ||
emitter.on("foo", function (foo) { | ||
expect(foo).to.equal("create"); | ||
done(); | ||
}).trigger("foo", "create"); | ||
}); | ||
}); | ||
describe("On and trigger", function () { | ||
@@ -279,3 +295,4 @@ var obj = { | ||
it("listenTo yourself cleans yourself up with stopListening", function () { | ||
it("listenTo yourself cleans yourself up with stopListening", | ||
function () { | ||
var e = Eventify.enable({}); | ||
@@ -426,3 +443,4 @@ e.listenTo(e, "foo", function () { | ||
obj.trigger('event'); | ||
it('should have been incremented the counter three times', function (done) { | ||
it('should have been incremented the counter three times', | ||
function (done) { | ||
expect(obj.counter).to.be(3); | ||
@@ -460,3 +478,4 @@ done(); | ||
describe("#1282 - 'all' callback list is retrieved after each event.", function () { | ||
describe("#1282 - 'all' callback list is retrieved after each event.", | ||
function () { | ||
var counter = 0, | ||
@@ -556,5 +575,7 @@ obj = Eventify.enable(); | ||
describe("When attaching an event listener only once, it:", function () { | ||
describe("When attaching an event listener only once, it:", | ||
function () { | ||
it("once", function () { | ||
// Same as the previous test, but we use once rather than having to explicitly unbind | ||
// Same as the previous test, but we use once rather than | ||
// having to explicitly unbind | ||
var incrA, incrB, | ||
@@ -576,4 +597,6 @@ obj = { | ||
obj.trigger('event'); | ||
expect(obj.counterA, 1, 'counterA should have only been incremented once.'); | ||
expect(obj.counterB, 1, 'counterB should have only been incremented once.'); | ||
expect(obj.counterA, 1, 'counterA should have only been ' + | ||
'incremented once.'); | ||
expect(obj.counterB, 1, 'counterB should have only been ' + | ||
'incremented once.'); | ||
}); | ||
@@ -715,2 +738,2 @@ | ||
}(this)); | ||
}(this)); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
227
46974
12
1006
1