Skip to content

Commit

Permalink
v0.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesplease committed Jan 28, 2015
1 parent 619686b commit ff88c34
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 36 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "backbone.radio",
"version": "0.8.5",
"version": "0.9.0",
"homepage": "https://github.com/marionettejs/backbone.radio",
"authors": [
"Jmeas <[email protected]>"
Expand Down
71 changes: 40 additions & 31 deletions build/backbone.radio.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Backbone.Radio v0.8.5
// Backbone.Radio v0.9.0
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(['backbone', 'underscore'], function(Backbone, _) {
Expand All @@ -20,7 +20,7 @@

var Radio = Backbone.Radio = {};

Radio.VERSION = '0.8.5';
Radio.VERSION = '0.9.0';

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

// Format debug text.
Radio._debugText = function(warning, eventName, channelName) {
return warning + (channelName ? ' on the ' + channelName + ' channel' : '') +
': "' + eventName + '"';
};

// This is the method that's called when an unregistered event was called.
// By default, it logs warning to the console. By overriding this you could
// make it throw an Error, for instance. This would make firing a nonexistent event
// have the same consequence as firing a nonexistent method on an Object.
function debugLog(warning, eventName, channelName) {
if (!Radio.DEBUG) { return; }
var channelText = channelName ? ' on the ' + channelName + ' channel' : '';
if (console && console.warn) {
console.warn(warning + channelText + ': "' + eventName + '"');
Radio.debugLog = function(warning, eventName, channelName) {
if (Radio.DEBUG && console && console.warn) {
console.warn(Radio._debugText(warning, eventName, channelName));
}
}
};

var eventSplitter = /\s+/;

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

var results = [];
var results = {};

// Handle event maps.
if (typeof name === 'object') {
for (var key in name) {
var result = obj[action].apply(obj, [key, name[key]].concat(rest));
eventSplitter.test(key) ? (results = results.concat(result)) : results.push(result);
eventSplitter.test(key) ? _.extend(results, result) : results[key] = result;
}
return results;
}
Expand All @@ -73,16 +77,16 @@
if (eventSplitter.test(name)) {
var names = name.split(eventSplitter);
for (var i = 0, l = names.length; i < l; i++) {
results.push(obj[action].apply(obj, [names[i]].concat(rest)));
results[names[i]] = obj[action].apply(obj, [names[i]].concat(rest));
}
return results;
}

return false;
}
};

// An optimized way to execute callbacks.
function callHandler(callback, context, args) {
Radio._callHandler = function(callback, context, args) {
var a1 = args[0], a2 = args[1], a3 = args[2];
switch(args.length) {
case 0: return callback.call(context);
Expand All @@ -91,7 +95,7 @@
case 3: return callback.call(context, a1, a2, a3);
default: return callback.apply(context, args);
}
}
};

// A helper used by `off` methods to the handler from the store
function removeHandler(store, name, callback, context) {
Expand Down Expand Up @@ -182,7 +186,7 @@
// Issue a command
command: function(name) {
var args = _.rest(arguments);
if (eventsApi(this, 'command', name, args)) {
if (Radio._eventsApi(this, 'command', name, args)) {
return this;
}
var channelName = this.channelName;
Expand All @@ -197,23 +201,23 @@
if (commands && (commands[name] || commands['default'])) {
var handler = commands[name] || commands['default'];
args = commands[name] ? args : arguments;
callHandler(handler.callback, handler.context, args);
Radio._callHandler(handler.callback, handler.context, args);
} else {
debugLog('An unhandled command was fired', name, channelName);
Radio.debugLog('An unhandled command was fired', name, channelName);
}

return this;
},

// Register a handler for a command.
comply: function(name, callback, context) {
if (eventsApi(this, 'comply', name, [callback, context])) {
if (Radio._eventsApi(this, 'comply', name, [callback, context])) {
return this;
}
this._commands || (this._commands = {});

if (this._commands[name]) {
debugLog('A command was overwritten', name, this.channelName);
Radio.debugLog('A command was overwritten', name, this.channelName);
}

this._commands[name] = {
Expand All @@ -226,7 +230,7 @@

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

// Remove handler(s)
stopComplying: function(name, callback, context) {
if (eventsApi(this, 'stopComplying', name)) {
if (Radio._eventsApi(this, 'stopComplying', name)) {
return this;
}

// Remove everything if there are no arguments passed
if (!name && !callback && !context) {
delete this._commands;
} else if (!removeHandlers(this._commands, name, callback, context)) {
debugLog('Attempted to remove the unregistered command', name, this.channelName);
Radio.debugLog('Attempted to remove the unregistered command', name, this.channelName);
}

return this;
Expand All @@ -272,7 +276,7 @@
// Make a request
request: function(name) {
var args = _.rest(arguments);
var results = eventsApi(this, 'request', name, args);
var results = Radio._eventsApi(this, 'request', name, args);
if (results) {
return results;
}
Expand All @@ -288,22 +292,22 @@
if (requests && (requests[name] || requests['default'])) {
var handler = requests[name] || requests['default'];
args = requests[name] ? args : arguments;
return callHandler(handler.callback, handler.context, args);
return Radio._callHandler(handler.callback, handler.context, args);
} else {
debugLog('An unhandled request was fired', name, channelName);
Radio.debugLog('An unhandled request was fired', name, channelName);
}
},

// Set up a handler for a request
reply: function(name, callback, context) {
if (eventsApi(this, 'reply', name, [callback, context])) {
if (Radio._eventsApi(this, 'reply', name, [callback, context])) {
return this;
}

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

if (this._requests[name]) {
debugLog('A request was overwritten', name, this.channelName);
Radio.debugLog('A request was overwritten', name, this.channelName);
}

this._requests[name] = {
Expand All @@ -316,7 +320,7 @@

// Set up a handler that can only be requested once
replyOnce: function(name, callback, context) {
if (eventsApi(this, 'replyOnce', name, [callback, context])) {
if (Radio._eventsApi(this, 'replyOnce', name, [callback, context])) {
return this;
}

Expand All @@ -332,15 +336,15 @@

// Remove handler(s)
stopReplying: function(name, callback, context) {
if (eventsApi(this, 'stopReplying', name)) {
if (Radio._eventsApi(this, 'stopReplying', name)) {
return this;
}

// Remove everything if there are no arguments passed
if (!name && !callback && !context) {
delete this._requests;
} else if (!removeHandlers(this._requests, name, callback, context)) {
debugLog('Attempted to remove the unregistered request', name, this.channelName);
Radio.debugLog('Attempted to remove the unregistered request', name, this.channelName);
}

return this;
Expand Down Expand Up @@ -412,6 +416,11 @@
});
});

Radio.reset = function(channelName) {
var channels = !channelName ? this._channels : [this._channels[channelName]];
_.invoke(channels, 'reset');
};


return Radio;
}));
4 changes: 2 additions & 2 deletions build/backbone.radio.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ff88c34

Please sign in to comment.