Last active
December 27, 2015 19:19
-
-
Save jtenner/7376285 to your computer and use it in GitHub Desktop.
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
!function(constructorObject){ | |
constructorObject.plugin(function(self, options){ | |
var events = {};//event parent | |
self.on = function(eventNameSpace, func){//add the function to the namespace | |
var event = events[eventNameSpace] = events[eventNameSpace]||[];//coalesce and make sure it's an array | |
func._function_id = func.name+Date.now();//give it some unique value for later | |
event.push(func);//store it | |
return self;//chain! | |
}; | |
self.trigger = function(eventNameSpace){//activate an event namespace | |
var event = events[eventNameSpace]||[], | |
args = [].slice.call(arguments, 1);//grab everything except the namespace and pop it into an array | |
event.forEach(function(func){//for each event | |
func.apply(self, args);//apply the arguments | |
}); | |
return self;//chain! | |
}; | |
self.off = func(eventNameSpace, func){//remove a queued event | |
var event = events[eventNameSpace]||[]; | |
for(var i = 0, _len = event.length;i<_len; i++){//for each event | |
var eventFunc = event[i];//grab the event | |
if(func._function_id === eventFunc._function_id){//compare id's | |
event.splice(i,1);//remove it from the queue completely | |
return self;//all done, don't loop any more | |
} | |
} | |
return self;//Chain! | |
}) | |
}); | |
}(constructorObject/*, additional plugin dependencies*/) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment