forked from chalbert/Backbone-Mediator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
backbone-mediator.js
201 lines (174 loc) · 4.94 KB
/
backbone-mediator.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* |-------------------|
* | Backbone-Mediator |
* |-------------------|
* Backbone-Mediator is freely distributable under the MIT license.
*
* <a href="https://github.com/chalbert/Backbone-Mediator">More details & documentation</a>
*
* @author Nicolas Gilbert
*
* @requires _
* @requires Backbone
*/
(function(factory){
'use strict';
if (typeof define === 'function' && define.amd) {
define(['underscore', 'backbone'], factory);
} else {
factory(_, Backbone);
}
})(function (_, Backbone){
'use strict';
/**
* @static
*/
var channels = {},
Subscriber,
/** @borrows Backbone.View#delegateEvents */
delegateEvents = Backbone.View.prototype.delegateEvents,
/** @borrows Backbone.View#delegateEvents */
undelegateEvents = Backbone.View.prototype.undelegateEvents;
/**
* @class
*/
Backbone.Mediator = {
/**
* Subscribe to a channel
*
* @param channel
*/
subscribe: function(channel, subscription, context, once) {
if (!channels[channel]) channels[channel] = [];
channels[channel].push({fn: subscription, context: context || this, once: once});
},
/**
* Trigger all callbacks for a channel
*
* @param channel
* @params N Extra parametter to pass to handler
*/
publish: function(channel) {
if (!channels[channel]) return;
var args = [].slice.call(arguments, 1),
subscription;
for (var i = 0; i < channels[channel].length; i++) {
subscription = channels[channel][i];
subscription.fn.apply(subscription.context, args);
if (subscription.once) {
Backbone.Mediator.unsubscribe(channel, subscription.fn, subscription.context);
i--;
}
}
},
/**
* Cancel subscription
*
* @param channel
* @param fn
* @param context
*/
unsubscribe: function(channel, fn, context){
if (!channels[channel]) return;
var subscription;
for (var i = 0; i < channels[channel].length; i++) {
subscription = channels[channel][i];
if (subscription.fn === fn && subscription.context === context) {
channels[channel].splice(i, 1);
i--;
}
}
},
/**
* Subscribing to one event only
*
* @param channel
* @param subscription
* @param context
*/
subscribeOnce: function (channel, subscription, context) {
Backbone.Mediator.subscribe(channel, subscription, context, true);
}
};
/**
* Allow to define convention-based subscriptions
* as an 'subscriptions' hash on a view. Subscriptions
* can then be easily setup and cleaned.
*
* @class
*/
Subscriber = {
/**
* Extend delegateEvents() to set subscriptions
*/
delegateEvents: function(){
delegateEvents.apply(this, arguments);
this.setSubscriptions();
},
/**
* Extend undelegateEvents() to unset subscriptions
*/
undelegateEvents: function(){
undelegateEvents.apply(this, arguments);
this.unsetSubscriptions();
},
/** @property {Object} List of subscriptions, to be defined */
subscriptions: {},
/**
* Subscribe to each subscription
* @param {Object} [subscriptions] An optional hash of subscription to add
*/
setSubscriptions: function(subscriptions){
if (subscriptions) _.extend(this.subscriptions || {}, subscriptions);
subscriptions = subscriptions || this.subscriptions;
if (!subscriptions || _.isEmpty(subscriptions)) return;
// Just to be sure we don't set duplicate
this.unsetSubscriptions(subscriptions);
_.each(subscriptions, function(subscription, channel){
var once;
if (subscription.$once) {
subscription = subscription.$once;
once = true;
}
if (_.isString(subscription)) {
subscription = this[subscription];
}
Backbone.Mediator.subscribe(channel, subscription, this, once);
}, this);
},
/**
* Unsubscribe to each subscription
* @param {Object} [subscriptions] An optional hash of subscription to remove
*/
unsetSubscriptions: function(subscriptions){
subscriptions = subscriptions || this.subscriptions;
if (!subscriptions || _.isEmpty(subscriptions)) return;
_.each(subscriptions, function(subscription, channel){
if (_.isString(subscription)) {
subscription = this[subscription];
}
Backbone.Mediator.unsubscribe(channel, subscription.$once || subscription, this);
}, this);
}
};
/**
* @lends Backbone.View.prototype
*/
_.extend(Backbone.View.prototype, Subscriber);
/**
* @lends Backbone.Mediator
*/
_.extend(Backbone.Mediator, {
/**
* Shortcut for publish
* @function
*/
pub: Backbone.Mediator.publish,
/**
* Shortcut for subscribe
* @function
*/
sub: Backbone.Mediator.subscribe
});
return Backbone;
});