This site has a good article that explains the creation and firing of custom javascript events without the use of 3rd party libraries.
Class
Class
var CustomEvent = function() { //name of the event this.eventName = arguments[0]; var mEventName = this.eventName; //function to call on event fire var eventAction = null; //subscribe a function to the event this.subscribe = function(fn) { eventAction = fn; }; //fire the event this.fire = function(sender, eventArgs) { this.eventName = eventName2; if(eventAction != null) { eventAction(sender, eventArgs); } else { alert('There was no function subscribed to the ' + mEventName + ' event!'); } }; Register event handler
var myEvent = new CustomEvent("my event"); myEvent.subscribe(function(sender, eventArgs) { alert(eventArgs.message); });
Fire custom eventmyEvent.fire(null, { message: 'you just witnessed the firing of a custom event called ' + this.eventName + '!' });
No comments:
Post a Comment