angular-tiny-eventemitter
Advanced tools
Comparing version 1.0.1 to 1.1.0
{ | ||
"name": "angular-tiny-eventemitter", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"main": "dist/angular-tiny-eventemitter.js", | ||
@@ -5,0 +5,0 @@ "ignore": [ |
angular.module('rt.eventemitter', []).factory('eventEmitter', function () { | ||
var key = '_listeners'; | ||
function on(event, fn) { | ||
function on($scope, event, fn) { | ||
if (typeof $scope === 'string') { | ||
fn = event; | ||
event = $scope; | ||
$scope = null; | ||
} | ||
if (!this[key]) { | ||
@@ -12,5 +17,16 @@ this[key] = {}; | ||
events[event].push(fn); | ||
var self = this; | ||
if ($scope) { | ||
$scope.$on('$destroy', function () { | ||
self.off(event, fn); | ||
}); | ||
} | ||
return this; | ||
} | ||
function once(event, fn) { | ||
function once($scope, event, fn) { | ||
if (typeof $scope === 'string') { | ||
fn = event; | ||
event = $scope; | ||
$scope = null; | ||
} | ||
var self = this; | ||
@@ -21,3 +37,3 @@ var cb = function () { | ||
}; | ||
this.on(event, cb); | ||
this.on($scope, event, cb); | ||
return this; | ||
@@ -24,0 +40,0 @@ } |
@@ -1,1 +0,1 @@ | ||
angular.module("rt.eventemitter",[]).factory("eventEmitter",function(){function a(a,b){this[e]||(this[e]={});var c=this[e];return c[a]||(c[a]=[]),c[a].push(b),this}function b(a,b){var c=this,d=function(){b.apply(this,arguments),c.off(a,d)};return this.on(a,d),this}function c(a,b){if(!this[e]||!this[e][a])return this;var c=this[e];if(b){var d=c[a],f=d.indexOf(b);f>-1&&d.splice(f,1)}else delete c[a];return this}function d(a){if(this[e]&&this[e][a]){for(var b=this[e][a].slice(0),c=[].slice.call(arguments,1),d=0;d<b.length;d++)b[d].apply(null,c);return this}}var e="_listeners";return{inject:function(e){var f=e.prototype||e;f.on=a,f.once=b,f.off=c,f.emit=d}}}); | ||
angular.module("rt.eventemitter",[]).factory("eventEmitter",function(){function a(a,b,c){"string"==typeof a&&(c=b,b=a,a=null),this[e]||(this[e]={});var d=this[e];d[b]||(d[b]=[]),d[b].push(c);var f=this;return a&&a.$on("$destroy",function(){f.off(b,c)}),this}function b(a,b,c){"string"==typeof a&&(c=b,b=a,a=null);var d=this,e=function(){c.apply(this,arguments),d.off(b,e)};return this.on(a,b,e),this}function c(a,b){if(!this[e]||!this[e][a])return this;var c=this[e];if(b){var d=c[a],f=d.indexOf(b);f>-1&&d.splice(f,1)}else delete c[a];return this}function d(a){if(this[e]&&this[e][a]){for(var b=this[e][a].slice(0),c=[].slice.call(arguments,1),d=0;d<b.length;d++)b[d].apply(null,c);return this}}var e="_listeners";return{inject:function(e){var f=e.prototype||e;f.on=a,f.once=b,f.off=c,f.emit=d}}}); |
{ | ||
"name": "angular-tiny-eventemitter", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Tiny event emitter for Angular.JS.", | ||
@@ -26,3 +26,3 @@ "main": "dist/angular-tiny-eventemitter.js", | ||
"grunt": "~0.4.4", | ||
"grunt-bump": "0.0.14", | ||
"grunt-bump": "0.3.1", | ||
"grunt-contrib-clean": "~0.5.0", | ||
@@ -29,0 +29,0 @@ "grunt-contrib-concat": "~0.4.0", |
@@ -48,4 +48,12 @@ # angular-tiny-eventemitter | ||
var thing = new MyType(123); | ||
thing.on('event', function (arg1, arg2) { | ||
function logTheEvent(arg1, arg2) { | ||
console.log('Got event: ' + arg1 + ', ' + arg2); | ||
} | ||
thing.on('event', logTheEvent); | ||
$scope.$on('$destroy', function () { | ||
// Remember to avoid memory leaks! | ||
thing.off('event', logTheEvent); | ||
}); | ||
@@ -57,2 +65,19 @@ | ||
Alternatively, you can pass a $scope as the first parameter to on() or once() and the listener will be automatically unregistered if the $scope is destroyed. | ||
```js | ||
angular.module('myApp').controller('TestCtrl', function ($scope, MyType) { | ||
var thing = new MyType(123); | ||
function logTheEvent(arg1, arg2) { | ||
console.log('Got event: ' + arg1 + ', ' + arg2); | ||
} | ||
// Passing $scope will register a $destroy event for you. | ||
thing.on($scope, 'event', logTheEvent); | ||
thing.emit('event', 4, 8); | ||
}); | ||
``` | ||
## License | ||
@@ -59,0 +84,0 @@ |
10483
129
104