Skip to content
This repository has been archived by the owner on Nov 17, 2020. It is now read-only.

Commit

Permalink
Merge pull request #36 from jasonLaster/radio
Browse files Browse the repository at this point in the history
Add radio to wreqr
  • Loading branch information
jasonLaster committed Apr 11, 2014
2 parents 31b2eea + 8f446db commit 73396c9
Show file tree
Hide file tree
Showing 15 changed files with 1,023 additions and 3 deletions.
7 changes: 5 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,12 @@ module.exports = function(grunt) {
vendor : [
'public/javascripts/json2.js',
'public/javascripts/jquery.js',
'public/javascripts/underscore.js',
'public/javascripts/backbone.js'
'node_modules/underscore/underscore.js',
'node_modules/backbone/backbone.js',
'node_modules/sinon/pkg/sinon.js',
'node_modules/jasmine-sinon/lib/jasmine-sinon.js',
],
keepRunner: true,
},
coverage : {
src : '<%= jasmine.wreqr.src %>',
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@
"grunt-contrib-connect": "0.2.0",
"grunt-template-jasmine-istanbul": "0.2.0",
"grunt-preprocess": "2.0.0",
"grunt-plato": "0.1.5"
"grunt-plato": "0.1.5",
"sinon": "1.9.0",
"jasmine-sinon": "0.3.1"
},
"jam":{
"dependencies": {
Expand Down
36 changes: 36 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,42 @@ var result = reqres.request("foo");
console.log(result);
```

### Radio

Radio is a convenient way for emitting events through channels. Radio can be used to either retrieve a channel, or talk through a channel with either command, reqres, or vent.

```js
// channels
var globalChannel = Backbone.Wreqr.radio.channel('global');
var userChannel = Backbone.Wreqr.radio.channel('user');

// Wreqr events
Backbone.Wreqr.radio.commands.execute( 'global', 'shutdown' );
Backbone.Wreqr.radio.reqres.request( 'global', 'current-user' );
Backbone.Wreqr.radio.vent.trigger( 'global', 'game-over');

```

### Channel
Channel is an object that wraps EventAggregator, Commands, and Reqres. Channels provide a convenient way for the objects in your system to talk to one another without the global channel becoming too noisy.

```js
// global channel
var globalChannel = Backbone.Wreqr.radio.channel('global');
globalChannel.commands.execute('shutdown' );
globalChannel.reqres.request('current-user' );
globalChannel.vent.trigger('game-over');

// user channel
var userChannel = Backbone.Wreqr.radio.channel('user');
userChannel.commands.execute('punnish');
userChannel.reqres.request('user-avatar');
userChannel.vent.trigger('win', {
level: 2,
stars: 3
});
```

### Adding Multiple Handlers

Multiple handlers can be set on the Commands and RequestResponse
Expand Down
41 changes: 41 additions & 0 deletions spec/javascripts/channel/connect-commands.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
describe('Executing `connectCommands` with a hash as the first argument', function() {

var
ch,
label1 = 'one',
label2 = 'two',
cbOne,
cbTwo,
p,
ret,
commandsHash;

beforeEach(function() {

cbOne = function() {};
cbTwo = function() {};
ch = Wreqr.radio.channel('foo');

commandsHash = {};
commandsHash[label2] = cbOne;
commandsHash[label1] = cbTwo;

ret = ch.connectCommands( commandsHash );

p = ch.commands._wreqrHandlers || {};

});

afterEach(function() {
ch.reset();
});

it( 'should attach the listeners to the Channel', function() {
expect(_.keys(p)).toEqual( [label2, label1] );
});

it( 'should return the Channel', function() {
expect( ret ).toBe( ch );
});

});
41 changes: 41 additions & 0 deletions spec/javascripts/channel/connect-events.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
describe('Executing `connectEvents` with a hash as the first argument', function() {

var
ch,
label1 = 'one',
label2 = 'two',
cbOne,
cbTwo,
p,
ret,
eventsHash;

beforeEach(function() {

cbOne = function() {};
cbTwo = function() {};
ch = Wreqr.radio.channel('test');

eventsHash = {};
eventsHash[label2] = cbOne;
eventsHash[label1] = cbTwo;

ret = ch.connectEvents( eventsHash );

p = ch.vent._events || {};

});

afterEach(function() {
ch.reset();
});

it( 'should attach the listeners to the Channel', function() {
expect(_.keys(p)).toEqual( [label2, label1] );
});

it( 'should return the Channel', function() {
expect( ret ).toBe( ch );
});

});
41 changes: 41 additions & 0 deletions spec/javascripts/channel/connect-requests.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
describe('Executing `connectRequests` with a hash as the first argument', function() {

var
ch,
label1 = 'one',
label2 = 'two',
cbOne,
cbTwo,
p,
ret,
requestsHash;

beforeEach(function() {

cbOne = function() {};
cbTwo = function() {};
ch = Wreqr.radio.channel('test');

requestsHash = {};
requestsHash[label2] = cbOne;
requestsHash[label1] = cbTwo;

ret = ch.connectRequests( requestsHash );

p = ch.reqres._wreqrHandlers || {};

});

afterEach(function() {
ch.reset();
});

it( 'should attach the listeners to the Channel', function() {
expect(_.keys(p)).toEqual( [label2, label1] );
});

it( 'should return the Channel', function() {
expect( ret ).toBe( ch );
});

});
23 changes: 23 additions & 0 deletions spec/javascripts/channel/create.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
describe('Creating a Channel', function() {

var
ch,
chName,
name = 'test',
v, c, r;

beforeEach(function() {
ch = new Wreqr.Channel( name );
});

it( 'should set the name', function() {
expect( ch.channelName ).toEqual( name );
});

it( 'should instantiate a new instance of each messaging system', function() {
expect( ch.vent instanceof Backbone.Wreqr.EventAggregator ).toBeTruthy();
expect( ch.commands instanceof Backbone.Wreqr.Commands ).toBeTruthy();
expect( ch.reqres instanceof Backbone.Wreqr.RequestResponse ).toBeTruthy();
});

});
44 changes: 44 additions & 0 deletions spec/javascripts/channel/reset.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
describe('Running `resetChannel`', function() {

var
ch,
v1Stub, v2Stub, cStub, rStub,
v, c, r,
ret;

beforeEach(function() {

ch = new Wreqr.Channel( 'test' );
v = ch.vent;
c = ch.commands;
r = ch.reqres;
v1Stub = sinon.spy( v, "off");
v2Stub = sinon.spy( v, "stopListening");
cStub = sinon.spy( c, "removeAllHandlers");
rStub = sinon.spy( r, "removeAllHandlers");

ret = ch.reset();

});

afterEach(function() {

v1Stub.restore();
v2Stub.restore();
cStub.restore();
rStub.restore();

});

it( 'should call the reset functions for each messaging system', function() {
expect( v1Stub ).toHaveBeenCalledOnce();
expect( v2Stub ).toHaveBeenCalledOnce();
expect( cStub ).toHaveBeenCalledOnce();
expect( rStub ).toHaveBeenCalledOnce();
});

it( 'should return the Channel', function() {
expect( ret ).toBe( ch );
});

});
42 changes: 42 additions & 0 deletions spec/javascripts/radio/channel.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
describe('radio.channel', function () {

var channel, channelName;

describe('with no arguments', function() {

it( 'should throw an exception', function() {
expect( function() { Wreqr.radio.channel()} ).toThrow();
});

});

describe('for a nonexistent channel', function() {

beforeEach(function() {
channel = Wreqr.radio.channel('lala');
});

it( 'should return an instance of the default channel', function() {
expect( channel.channelName ).toEqual( 'lala' );
});

});

describe('twice with the same name', function() {

var chOne, chTwo;

beforeEach(function() {

chOne = Wreqr.radio.channel( 'lala' );
chTwo = Wreqr.radio.channel( 'lala' );

});

it( 'should return the same channel', function() {
expect( chOne ).toBe( chTwo );
});

});

});
Loading

0 comments on commit 73396c9

Please sign in to comment.