Skip to content

Commit b08cb7f

Browse files
committed
v0.9.0
1 parent 619686b commit b08cb7f

File tree

5 files changed

+45
-36
lines changed

5 files changed

+45
-36
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "backbone.radio",
3-
"version": "0.8.5",
3+
"version": "0.9.0",
44
"homepage": "https://github.com/marionettejs/backbone.radio",
55
"authors": [
66
"Jmeas <jellyes2@gmail.com>"

build/backbone.radio.js

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Backbone.Radio v0.8.5
1+
// Backbone.Radio v0.9.0
22
(function(root, factory) {
33
if (typeof define === 'function' && define.amd) {
44
define(['backbone', 'underscore'], function(Backbone, _) {
@@ -20,7 +20,7 @@
2020

2121
var Radio = Backbone.Radio = {};
2222

23-
Radio.VERSION = '0.8.5';
23+
Radio.VERSION = '0.9.0';
2424

2525
// This allows you to run multiple instances of Radio on the same
2626
// webapp. After loading the new version, call `noConflict()` to
@@ -35,36 +35,40 @@
3535
// get around the issues of lack of warnings when events are mis-typed.
3636
Radio.DEBUG = false;
3737

38+
// Format debug text.
39+
Radio._debugText = function(warning, eventName, channelName) {
40+
return warning + (channelName ? ' on the ' + channelName + ' channel' : '') +
41+
': "' + eventName + '"';
42+
};
43+
3844
// This is the method that's called when an unregistered event was called.
3945
// By default, it logs warning to the console. By overriding this you could
4046
// make it throw an Error, for instance. This would make firing a nonexistent event
4147
// have the same consequence as firing a nonexistent method on an Object.
42-
function debugLog(warning, eventName, channelName) {
43-
if (!Radio.DEBUG) { return; }
44-
var channelText = channelName ? ' on the ' + channelName + ' channel' : '';
45-
if (console && console.warn) {
46-
console.warn(warning + channelText + ': "' + eventName + '"');
48+
Radio.debugLog = function(warning, eventName, channelName) {
49+
if (Radio.DEBUG && console && console.warn) {
50+
console.warn(Radio._debugText(warning, eventName, channelName));
4751
}
48-
}
52+
};
4953

5054
var eventSplitter = /\s+/;
5155

5256
// An internal method used to handle Radio's method overloading for Requests and
5357
// Commands. It's borrowed from Backbone.Events. It differs from Backbone's overload
5458
// API (which is used in Backbone.Events) in that it doesn't support space-separated
5559
// event names.
56-
function eventsApi(obj, action, name, rest) {
60+
Radio._eventsApi = function(obj, action, name, rest) {
5761
if (!name) {
5862
return false;
5963
}
6064

61-
var results = [];
65+
var results = {};
6266

6367
// Handle event maps.
6468
if (typeof name === 'object') {
6569
for (var key in name) {
6670
var result = obj[action].apply(obj, [key, name[key]].concat(rest));
67-
eventSplitter.test(key) ? (results = results.concat(result)) : results.push(result);
71+
eventSplitter.test(key) ? _.extend(results, result) : results[key] = result;
6872
}
6973
return results;
7074
}
@@ -73,16 +77,16 @@
7377
if (eventSplitter.test(name)) {
7478
var names = name.split(eventSplitter);
7579
for (var i = 0, l = names.length; i < l; i++) {
76-
results.push(obj[action].apply(obj, [names[i]].concat(rest)));
80+
results[names[i]] = obj[action].apply(obj, [names[i]].concat(rest));
7781
}
7882
return results;
7983
}
8084

8185
return false;
82-
}
86+
};
8387

8488
// An optimized way to execute callbacks.
85-
function callHandler(callback, context, args) {
89+
Radio._callHandler = function(callback, context, args) {
8690
var a1 = args[0], a2 = args[1], a3 = args[2];
8791
switch(args.length) {
8892
case 0: return callback.call(context);
@@ -91,7 +95,7 @@
9195
case 3: return callback.call(context, a1, a2, a3);
9296
default: return callback.apply(context, args);
9397
}
94-
}
98+
};
9599

96100
// A helper used by `off` methods to the handler from the store
97101
function removeHandler(store, name, callback, context) {
@@ -182,7 +186,7 @@
182186
// Issue a command
183187
command: function(name) {
184188
var args = _.rest(arguments);
185-
if (eventsApi(this, 'command', name, args)) {
189+
if (Radio._eventsApi(this, 'command', name, args)) {
186190
return this;
187191
}
188192
var channelName = this.channelName;
@@ -197,23 +201,23 @@
197201
if (commands && (commands[name] || commands['default'])) {
198202
var handler = commands[name] || commands['default'];
199203
args = commands[name] ? args : arguments;
200-
callHandler(handler.callback, handler.context, args);
204+
Radio._callHandler(handler.callback, handler.context, args);
201205
} else {
202-
debugLog('An unhandled command was fired', name, channelName);
206+
Radio.debugLog('An unhandled command was fired', name, channelName);
203207
}
204208

205209
return this;
206210
},
207211

208212
// Register a handler for a command.
209213
comply: function(name, callback, context) {
210-
if (eventsApi(this, 'comply', name, [callback, context])) {
214+
if (Radio._eventsApi(this, 'comply', name, [callback, context])) {
211215
return this;
212216
}
213217
this._commands || (this._commands = {});
214218

215219
if (this._commands[name]) {
216-
debugLog('A command was overwritten', name, this.channelName);
220+
Radio.debugLog('A command was overwritten', name, this.channelName);
217221
}
218222

219223
this._commands[name] = {
@@ -226,7 +230,7 @@
226230

227231
// Register a handler for a command that happens just once.
228232
complyOnce: function(name, callback, context) {
229-
if (eventsApi(this, 'complyOnce', name, [callback, context])) {
233+
if (Radio._eventsApi(this, 'complyOnce', name, [callback, context])) {
230234
return this;
231235
}
232236
var self = this;
@@ -241,15 +245,15 @@
241245

242246
// Remove handler(s)
243247
stopComplying: function(name, callback, context) {
244-
if (eventsApi(this, 'stopComplying', name)) {
248+
if (Radio._eventsApi(this, 'stopComplying', name)) {
245249
return this;
246250
}
247251

248252
// Remove everything if there are no arguments passed
249253
if (!name && !callback && !context) {
250254
delete this._commands;
251255
} else if (!removeHandlers(this._commands, name, callback, context)) {
252-
debugLog('Attempted to remove the unregistered command', name, this.channelName);
256+
Radio.debugLog('Attempted to remove the unregistered command', name, this.channelName);
253257
}
254258

255259
return this;
@@ -272,7 +276,7 @@
272276
// Make a request
273277
request: function(name) {
274278
var args = _.rest(arguments);
275-
var results = eventsApi(this, 'request', name, args);
279+
var results = Radio._eventsApi(this, 'request', name, args);
276280
if (results) {
277281
return results;
278282
}
@@ -288,22 +292,22 @@
288292
if (requests && (requests[name] || requests['default'])) {
289293
var handler = requests[name] || requests['default'];
290294
args = requests[name] ? args : arguments;
291-
return callHandler(handler.callback, handler.context, args);
295+
return Radio._callHandler(handler.callback, handler.context, args);
292296
} else {
293-
debugLog('An unhandled request was fired', name, channelName);
297+
Radio.debugLog('An unhandled request was fired', name, channelName);
294298
}
295299
},
296300

297301
// Set up a handler for a request
298302
reply: function(name, callback, context) {
299-
if (eventsApi(this, 'reply', name, [callback, context])) {
303+
if (Radio._eventsApi(this, 'reply', name, [callback, context])) {
300304
return this;
301305
}
302306

303307
this._requests || (this._requests = {});
304308

305309
if (this._requests[name]) {
306-
debugLog('A request was overwritten', name, this.channelName);
310+
Radio.debugLog('A request was overwritten', name, this.channelName);
307311
}
308312

309313
this._requests[name] = {
@@ -316,7 +320,7 @@
316320

317321
// Set up a handler that can only be requested once
318322
replyOnce: function(name, callback, context) {
319-
if (eventsApi(this, 'replyOnce', name, [callback, context])) {
323+
if (Radio._eventsApi(this, 'replyOnce', name, [callback, context])) {
320324
return this;
321325
}
322326

@@ -332,15 +336,15 @@
332336

333337
// Remove handler(s)
334338
stopReplying: function(name, callback, context) {
335-
if (eventsApi(this, 'stopReplying', name)) {
339+
if (Radio._eventsApi(this, 'stopReplying', name)) {
336340
return this;
337341
}
338342

339343
// Remove everything if there are no arguments passed
340344
if (!name && !callback && !context) {
341345
delete this._requests;
342346
} else if (!removeHandlers(this._requests, name, callback, context)) {
343-
debugLog('Attempted to remove the unregistered request', name, this.channelName);
347+
Radio.debugLog('Attempted to remove the unregistered request', name, this.channelName);
344348
}
345349

346350
return this;
@@ -412,6 +416,11 @@
412416
});
413417
});
414418

419+
Radio.reset = function(channelName) {
420+
var channels = !channelName ? this._channels : [this._channels[channelName]];
421+
_.invoke(channels, 'reset');
422+
};
423+
415424

416425
return Radio;
417426
}));

build/backbone.radio.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)