-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpubSub.js
50 lines (40 loc) · 1.44 KB
/
pubSub.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* Author: Abraham Itule */
/*
* Create a custom way of raising/listening to Custom Events.
*/
(window.APP = (window.APP || {})).pubSub = {
/*
* Registers an event
*
* @param {String} ev - Name of the event
* @param {Function} callback - function code that will execute when the event is raised
* @return {Object} this - 'this' here points to the pubSub object
*/
subscribe: function(evntName, callback) {
var calls = (this._callbacks || (this._callbacks = {}));
((this._callbacks[evntName]) || (this._callbacks[evntName] = [])).push(callback)
return this
},
/*
* Raises/Fires an event
*
* @param {String} 'name' - The FIRST argument is reserved for the event name to raise/fire
* @param {ANY} 'args' - The rest of the following argumets (if provided) are arguments
to the callback that will listen to the event
* @return {Object} this - 'this' here points to the pubSub object
*/
on: function() {
// get arguments. To do so best, convert it to a proper array.
var args = Array.prototype.slice.call(arguments)
// get the name of the event
var eventName = args.shift()
if(!this._callbacks) return this
if(!this._callbacks[eventName] instanceof Array) return this
if(this._callbacks[eventName] == 'undefined') return this
var registeredCallbacks = this._callbacks[eventName]
for (var i = 0; i < registeredCallbacks.length; i++) {
registeredCallbacks[i].apply(this, args)
}
return this
}
};