You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
1.7 KiB
JavaScript
86 lines
1.7 KiB
JavaScript
// Wreqr.Radio
|
|
// --------------
|
|
//
|
|
// An object that lets you communicate with many channels.
|
|
Wreqr.radio = (function(Wreqr, _){
|
|
"use strict";
|
|
|
|
var Radio = function() {
|
|
this._channels = {};
|
|
this.vent = {};
|
|
this.commands = {};
|
|
this.reqres = {};
|
|
this._proxyMethods();
|
|
};
|
|
|
|
_.extend(Radio.prototype, {
|
|
|
|
channel: function(channelName) {
|
|
if (!channelName) {
|
|
throw new Error('Channel must receive a name');
|
|
}
|
|
|
|
return this._getChannel( channelName );
|
|
},
|
|
|
|
_getChannel: function(channelName) {
|
|
var channel = this._channels[channelName];
|
|
|
|
if(!channel) {
|
|
channel = new Wreqr.Channel(channelName);
|
|
this._channels[channelName] = channel;
|
|
}
|
|
|
|
return channel;
|
|
},
|
|
|
|
_proxyMethods: function() {
|
|
_.each(['vent', 'commands', 'reqres'], function(system) {
|
|
_.each( messageSystems[system], function(method) {
|
|
this[system][method] = proxyMethod(this, system, method);
|
|
}, this);
|
|
}, this);
|
|
}
|
|
});
|
|
|
|
|
|
var messageSystems = {
|
|
vent: [
|
|
'on',
|
|
'off',
|
|
'trigger',
|
|
'once',
|
|
'stopListening',
|
|
'listenTo',
|
|
'listenToOnce'
|
|
],
|
|
|
|
commands: [
|
|
'execute',
|
|
'setHandler',
|
|
'setHandlers',
|
|
'removeHandler',
|
|
'removeAllHandlers'
|
|
],
|
|
|
|
reqres: [
|
|
'request',
|
|
'setHandler',
|
|
'setHandlers',
|
|
'removeHandler',
|
|
'removeAllHandlers'
|
|
]
|
|
};
|
|
|
|
var proxyMethod = function(radio, system, method) {
|
|
return function(channelName) {
|
|
var messageSystem = radio._getChannel(channelName)[system];
|
|
|
|
return messageSystem[method].apply(messageSystem, _.rest(arguments));
|
|
};
|
|
};
|
|
|
|
return new Radio();
|
|
|
|
})(Wreqr, _);
|