Skip to content

Commit

Permalink
Store collections using the "current" link when it's available
Browse files Browse the repository at this point in the history
  • Loading branch information
uglymunky committed Apr 29, 2014
1 parent 7d046fb commit eb1f562
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/backbone.siren.store.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ Store.prototype = {
* @return {Backbone.Siren.Model}
*/
add: function (bbSirenObj) {
var self = this;
var self = this
, index;

if (Backbone.Siren.isCollection(bbSirenObj)) {
bbSirenObj.each(function (sirenModel) {
self.add(sirenModel);
});

index = bbSirenObj.link('current');
}

this.data[bbSirenObj.url()] = bbSirenObj;
this.data[index || bbSirenObj.url()] = bbSirenObj;
return bbSirenObj;
}

Expand Down
26 changes: 26 additions & 0 deletions test/spec/backbone.siren.store.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ describe('Backbone.Siren.Store: ', function () {
expect(store.data['http://two']).toBeDefined();
expect(store.data['http://three']).toBeDefined();
});


it('adds a collection to the store, indexing by "current" if available', function () {
var store = new Backbone.Siren.Store()
, bbSirenCollection = new Backbone.Siren.Collection({
'class': ['collection']
, entities: [
{properties: {}, links: [{rel: ['self'], href: 'http://one'}]}
, {properties: {}, links: [{rel: ['self'], href: 'http://two'}]}
, {properties: {}, links: [{rel: ['self'], href: 'http://three'}]}
]
, links: [
{
rel: ['self']
, href: 'http://collect'
}
, {
rel: ['current']
, href: 'http://collect?page=30'
}
]
});

store.add(bbSirenCollection);
expect(store.data['http://collect?page=30']).toBeDefined();
});
});


Expand Down

0 comments on commit eb1f562

Please sign in to comment.