Skip to content

Commit 09ff1a4

Browse files
committed
Add top-level reset method.
Resolves #175
1 parent b41ba31 commit 09ff1a4

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,23 @@ may overwrite it with your own logging message if you wish.
421421

422422
### 'Top-level' API
423423

424-
If you'd like to execute a method on a channel, yet you don't need to keep a handle of the channel around, you can do so with the proxy
425-
functions directly on the `Backbone.Radio` object.
424+
If you'd like to execute a method on a channel, yet you don't need to keep a handle of the
425+
channel around, you can do so with the proxy functions directly on the `Backbone.Radio` object.
426426

427427
```js
428428
// Trigger 'some:event' on the settings channel
429429
Backbone.Radio.trigger('settings', 'some:event');
430430
```
431431

432432
All of the methods for all three messaging systems are available from the top-level API.
433+
434+
#### `reset( [channelName] )`
435+
436+
You can also reset a single channel, or all Channels, from the `Radio` object directly. Pass a
437+
`channelName` to reset just that specific channel, or call the method without any arguments
438+
to reset every channel.
439+
440+
```js
441+
// Reset all channels
442+
Radio.reset();
443+
```

src/backbone.radio.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,8 @@ _.each(systems, function(system) {
392392
};
393393
});
394394
});
395+
396+
Radio.reset = function(channelName) {
397+
var channels = !channelName ? this._channels : [this._channels[channelName]];
398+
_.invoke(channels, 'reset');
399+
};

test/setup/helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function setupTestHelpers() {
77

88
afterEach(function() {
99
Backbone.Radio.DEBUG = false;
10-
_.invoke(Backbone.Radio._channels, 'reset');
10+
Backbone.Radio.reset();
1111
this.sinon.restore();
1212
delete global.stub;
1313
delete global.spy;

test/spec/top-level.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,43 @@ describe('Top-level API:', function() {
44
stub(this.channel);
55
});
66

7+
describe('Reset', function() {
8+
beforeEach(function() {
9+
this.channelOne = Backbone.Radio.channel('channelOne');
10+
this.channelTwo = Backbone.Radio.channel('channelTwo');
11+
12+
stub(this.channelOne, 'reset');
13+
stub(this.channelTwo, 'reset');
14+
});
15+
16+
describe('when passing a channel name', function() {
17+
beforeEach(function() {
18+
Backbone.Radio.reset('channelOne');
19+
});
20+
21+
it('should reset that channel', function() {
22+
expect(this.channelOne.reset).to.have.been.calledOnce;
23+
});
24+
25+
it('should not reset the other channels', function() {
26+
expect(this.channel.reset).to.not.have.been.called;
27+
expect(this.channelTwo.reset).to.not.have.been.called;
28+
});
29+
});
30+
31+
describe('when not passing a channel name', function() {
32+
beforeEach(function() {
33+
Backbone.Radio.reset();
34+
});
35+
36+
it('should reset all channels', function() {
37+
expect(this.channel.reset).to.have.been.calledOnce;
38+
expect(this.channelOne.reset).to.have.been.calledOnce;
39+
expect(this.channelTwo.reset).to.have.been.calledOnce;
40+
});
41+
});
42+
});
43+
744
describe('when executing Commands methods', function() {
845
beforeEach(function() {
946
Backbone.Radio.comply('myChannel', 'some:command', 'firstArg1', 'secondArg1');

0 commit comments

Comments
 (0)