-
Notifications
You must be signed in to change notification settings - Fork 0
/
intercom.js
45 lines (41 loc) · 1.3 KB
/
intercom.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
// ///////////////////////////////////////////////////////////////////////////
// Intercom - JavaScript mediator
// Jared Smith - 2014
// Based on the previous work of Ryan Florence (https://github.com/rpflorence)
// ///////////////////////////////////////////////////////////////////////////
var Intercom = (function() {
var listen = function(channel, fn) {
if(!Intercom.channels[channel]) Intercom.channels[channel] = [];
Intercom.channels[channel].push({context: this, callback: fn});
return this;
},
disconnect = function(channel) {
if(!Intercom.channels[channel]) return false;
delete Intercom.channels[channel];
return this;
},
broadcast = function(channel) {
if(!Intercom.channels[channel]) return false;
var args = Array.prototype.slice.call(arguments, 1);
for(var i = 0, l = Intercom.channels[channel].length; i < l; i++) {
var tuneIn = Intercom.channels[channel][i];
if (typeof tuneIn.callback == 'function') {
tuneIn.callback.apply(tuneIn.context, args);
} else {
tuneIn.context[tuneIn.callback].apply(tuneIn.context, args);
}
}
return this;
};
return {
channels: {},
listen: listen,
disconnect: disconnect,
broadcast: broadcast,
installOn: function(obj) {
obj.listen = listen;
obj.disconnect = disconnect;
obj.broadcast = broadcast;
}
};
})();