Skip to content

Commit a316be5

Browse files
jamiebuildsjamesplease
authored andcommitted
Return object from request with multiple values
1 parent b0f3fbe commit a316be5

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

src/backbone.radio.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ Radio._eventsApi = function(obj, action, name, rest) {
4444
return false;
4545
}
4646

47-
var results = [];
47+
var results = {};
4848

4949
// Handle event maps.
5050
if (typeof name === 'object') {
5151
for (var key in name) {
5252
var result = obj[action].apply(obj, [key, name[key]].concat(rest));
53-
eventSplitter.test(key) ? (results = results.concat(result)) : results.push(result);
53+
eventSplitter.test(key) ? _.extend(results, result) : results[key] = result;
5454
}
5555
return results;
5656
}
@@ -59,7 +59,7 @@ Radio._eventsApi = function(obj, action, name, rest) {
5959
if (eventSplitter.test(name)) {
6060
var names = name.split(eventSplitter);
6161
for (var i = 0, l = names.length; i < l; i++) {
62-
results.push(obj[action].apply(obj, [names[i]].concat(rest)));
62+
results[names[i]] = obj[action].apply(obj, [names[i]].concat(rest));
6363
}
6464
return results;
6565
}

test/spec/requests.js

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,12 @@ describe('Requests:', function() {
378378
.and.calledWith('requestTwo', 'argTwo');
379379
});
380380

381-
it('should return an array of replies', function() {
381+
it('should return an object of replies', function() {
382382
expect(this.Requests.request)
383-
.to.have.returned(['replyOne', 'replyTwo']);
383+
.to.have.returned({
384+
requestOne: 'replyOne',
385+
requestTwo: 'replyTwo'
386+
});
384387
});
385388
});
386389

@@ -468,9 +471,12 @@ describe('Requests:', function() {
468471
.and.calledWith('requestTwo', 'argOne', 'argTwo');
469472
});
470473

471-
it('should return an array of replies', function() {
474+
it('should return an object of replies', function() {
472475
expect(this.Requests.request)
473-
.to.have.returned(['replyOne', 'replyTwo']);
476+
.to.have.returned({
477+
requestOne: 'replyOne',
478+
requestTwo: 'replyTwo'
479+
});
474480
});
475481
});
476482

@@ -532,9 +538,39 @@ describe('Requests:', function() {
532538
.and.calledWith('requestThree', 'argTwo');
533539
});
534540

535-
it('should return an array of replies', function() {
541+
it('should return an object of replies', function() {
536542
expect(this.Requests.request)
537-
.to.have.returned(['replyOne', 'replyTwo', 'replyThree']);
543+
.to.have.returned({
544+
requestOne: 'replyOne',
545+
requestTwo: 'replyTwo',
546+
requestThree: 'replyThree'
547+
});
548+
});
549+
});
550+
551+
describe('when calling `request` with object with space-separated keys of requests with matching with matching keys', function() {
552+
beforeEach(function() {
553+
this.Requests.reply('requestOne requestTwo', _.identity);
554+
this.Requests.request({
555+
'requestOne requestTwo' : 'argOne',
556+
'requestTwo' : 'argTwo'
557+
});
558+
});
559+
560+
it('should call the set of requests', function() {
561+
expect(this.Requests.request)
562+
.to.have.callCount(5)
563+
.and.calledWith('requestOne', 'argOne')
564+
.and.calledWith('requestTwo', 'argOne')
565+
.and.calledWith('requestTwo', 'argTwo');
566+
});
567+
568+
it('should return an object of replies override duplicate keys', function() {
569+
expect(this.Requests.request)
570+
.to.have.returned({
571+
requestOne: 'argOne',
572+
requestTwo: 'argTwo'
573+
});
538574
});
539575
});
540576
});

0 commit comments

Comments
 (0)