-
-
Save Teamandy/3c34f5fd1b2f90a40e6ae285723c6f6c to your computer and use it in GitHub Desktop.
Revisions
-
learncodeacademy created this gist
Jul 29, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ //events - a super-basic Javascript (publish subscribe) pattern var events = { events: {}, on: function (eventName, fn) { this.events[eventName] = this.events[eventName] || []; this.events[eventName].push(fn); }, off: function(eventName, fn) { if (this.events[eventName]) { for (var i = 0; i < this.events[eventName].length; i++) { if (this.events[eventName][i] === fn) { this.events[eventName].splice(i, 1); break; } }; } }, emit: function (eventName, data) { if (this.events[eventName]) { this.events[eventName].forEach(function(fn) { fn(data); }); } } };