Created
January 25, 2011 01:23
-
-
Save totty90/794351 to your computer and use it in GitHub Desktop.
Javascript: inspired from actionscript 3 signals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Signal = function(){} | |
Signal.prototype = { | |
init: function(){ | |
this.listeners = []; | |
this.looping = false; | |
this.length = 0; | |
}, | |
add: function(method, applyOn){ | |
if(!method) return; | |
this.listeners[this.length] = {method: method, applyOn: applyOn}; | |
this.length += 1; | |
}, | |
remove: function(method){ | |
if(this.looping){ | |
this.listeners[this.listeners.indexOf(method)] = false; | |
this.length -= 1; | |
}else{ | |
this.listeners.splice(this.listeners.indexOf(method), 1); | |
} | |
}, | |
removeAll: function(){ | |
this.listeners = []; | |
this.length = 0; | |
}, | |
destroy: function(){ | |
this.removeAll(); | |
delete this.listeners; | |
}, | |
dispatch: function(){ | |
var listener; | |
this.looping = true; | |
for(var i = 0; i < this.length; i++){ | |
if(typeof this.listeners[i] === "object"){ | |
listener = this.listeners[i]; | |
listener.method.apply(listener.applyOn, arguments); | |
} | |
} | |
this.looping = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment