Skip to content

Commit

Permalink
Merge d104ad4 into aeef45e
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel committed May 17, 2014
2 parents aeef45e + d104ad4 commit 773e1a2
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
9 changes: 2 additions & 7 deletions src/backbone.siren.action.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,13 @@ Action.prototype = {
, attributes = options.attributes// So you can pass in properties that do not exist in the parent.
, actionName = this.name
, parent = this.parent
, store = parent && parent.siren && parent.siren.store
, presets = {
url: this.href
, actionName: actionName
, success: function (model, resp, options) {
parent.trigger('sync:' + actionName, model, resp, options);
if (parent instanceof Backbone.Model) {
parent.attributes = {};
parent.set(actionModel.attributes);
} else {
// Parent is assumed to be a collection
parent.set(actionModel.models);
}
Backbone.Siren.parse(resp, {store: store});
}
, error: function (model, xhr, options) {
parent.trigger('error:' + actionName, model, xhr, options);
Expand Down
16 changes: 13 additions & 3 deletions src/backbone.siren.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ function getRawEntitySelfUrl(rawEntity) {
} else if (rawEntity.links) {
url = getRawEntityUrl(rawEntity, 'self');
} else {
warn('Missing href or "self" link');
url = '';
}

Expand Down Expand Up @@ -468,6 +467,17 @@ _.extend(BbSiren, {
}


/**
* A js object is assumed to be a Siren object if it has a "self" url or top level "href".
*
* @param obj
* @returns {boolean}
*/
, isRawSiren: function (obj) {
return !!getRawEntitySelfUrl(obj);
}


/**
*
* @param {Backbone.Siren.Store} store
Expand Down Expand Up @@ -594,7 +604,7 @@ _.extend(BbSiren, {
*
* @param {Object} rawEntity
* @param {Object} options
* @returns {Backbone.Siren.Model|Backbone.Siren.Collection|Backbone.Siren.Error}
* @returns {Backbone.Siren.Model|Backbone.Siren.Collection|Backbone.Siren.Error|undefined}
*/
, parse: function (rawEntity, options) {
options = options || {};
Expand All @@ -605,7 +615,7 @@ _.extend(BbSiren, {
// @todo how should we represent errors? For now, treat them as regular Models...
// @todo are we storing errors in the store? If so, don't...
return new Backbone.Siren.Model(rawEntity, options);
} else {
} else if (BbSiren.isRawSiren) {
return this.parseModel(rawEntity, options);
}
}
Expand Down
25 changes: 25 additions & 0 deletions test/spec/backbone.siren.action.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,31 @@ describe('Siren Action: ', function () {

expect(callback).toHaveBeenCalled();
});


it('updates any cached entities from the response', function () {
$.ajax.restore();

var rawCollection = {'class': ['collection'], href: 'http://x.io/collection', actions: [sirenAction]};
var sirenCollection = new Backbone.Siren.Collection(rawCollection);

// Add a mock store
sirenCollection.siren.store = {
get: function () {
return sirenCollection;
}
};

this.stub(sirenCollection, 'update');

var server = sinon.fakeServer.create();
server.respondWith(JSON.stringify(rawCollection));

sirenCollection.getActionByName('add-item').execute();
server.respond();

expect(sirenCollection.update).toHaveBeenCalled();
});
});


Expand Down
5 changes: 1 addition & 4 deletions test/spec/backbone.siren.collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,9 @@ describe('Siren Collection: ', function () {
});


it('returns an empty string and warns if there is no url', function () {
it('returns an empty string if there is no url', function () {
var mySirenCollection = new Backbone.Siren.Collection({});
this.stub(console, 'warn');

expect(mySirenCollection.url()).toBe('');
expect(console.warn).toHaveBeenCalledOnce();
});
});

Expand Down
5 changes: 1 addition & 4 deletions test/spec/backbone.siren.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,9 @@ describe('Siren Model: ', function () {
});


it('returns an empty string and warns if there is no url', function () {
it('returns an empty string if there is no url', function () {
var mySirenModel = new Backbone.Siren.Model({});
this.stub(console, 'warn');

expect(mySirenModel.url()).toBe('');
expect(console.warn).toHaveBeenCalledOnce();
});
});

Expand Down

0 comments on commit 773e1a2

Please sign in to comment.