Skip to content

Commit

Permalink
Merge 0c8a735 into aeef45e
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel committed May 16, 2014
2 parents aeef45e + 0c8a735 commit 5fdfad4
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 80 deletions.
10 changes: 5 additions & 5 deletions src/backbone.siren.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ _.extend(BbSiren, {
}

if (model) {
model.update(rawModel);
model.update(rawModel, options);
} else {
model = new Backbone.Siren.Model(rawModel, options);
}
Expand All @@ -564,7 +564,7 @@ _.extend(BbSiren, {
if (store) {
collection = store.get(rawCollection);
if (collection) {
collection.update(rawCollection);
collection.update(rawCollection, options);
createNewCollectionFlag = false;
options.storeCurrentOnly = true;
}
Expand All @@ -574,7 +574,7 @@ _.extend(BbSiren, {
if (currentUrl) {
collection = store.get(currentUrl);
if (collection) {
collection.update(rawCollection);
collection.update(rawCollection, options);
} else {
createNewCollectionFlag = true;
}
Expand Down Expand Up @@ -1113,9 +1113,9 @@ _.extend(BbSiren, {
* @param {Object} rawCollection
* @param {Array} [models] When parsing, use these models instead of the raw models from the collection
*/
, update: function (rawCollection, models) {
, update: function (rawCollection, options) {
if (BbSiren.isLoaded(rawCollection)) {
this.add(this.parse(rawCollection, {preParsedModels: models}));
this.add(this.parse(rawCollection, options));
this.parseActions();
}

Expand Down
188 changes: 113 additions & 75 deletions test/spec/backbone.siren.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,80 +7,86 @@ describe('Backbone.Siren: ', function () {
// @todo quick fix for upgrade to buster 0.7
var expect = buster.expect;

var rawSettingsModel = {
"class":["order", "special"]
,"properties":{"orderNumber":42,"itemCount":3,"status":"pending"}
,"entities":[
{"class":["items","collection"],"rel":["http://x.io/rels/order-items", "name:order-items"],"href":"http://api.x.io/orders/42/items"}
,{"class":["info","customer"],"rel":["http://x.io/rels/customer", "name:customer"],"properties":{"customerId":"pj123","name":"Peter Joseph"},"links":[{"rel":["self"],"href":"http://api.x.io/customers/pj123"}]}
]
,"actions":[
{"name":"add-item","title":"Add Item","method":"POST","href":"http://api.x.io/orders/42/items","type":"application/x-www-form-urlencoded","fields":[{name: "addedLater"}, {"name":"orderNumber","type":"hidden","value":"42"},{"name":"productCode","type":"text"},{"name":"quantity","type":"number"}]}]
,"links":[
{"rel":["self"],"href":"http://api.x.io/orders/42"}
,{"rel":["previous"],"href":"http://api.x.io/orders/41"}
,{"rel":["next"],"href":"http://api.x.io/orders/43"}
]
};

var rawCollection = {
"class": ["collection"]
, "entities": [
{
"links": [
{"rel": ["self"], "href":"http://api.x.io/orders/41"}
]
}
, {
"links": [
{"rel": ["self"], "href":"http://api.x.io/orders/42"}
]
}
]
, links: [
{"rel": ["self"], "href":"http://api.x.io/orders"}
]
};


var rawCurrentCollection = {
entities: [
{
"links": [
{"rel": ["self"], "href":"http://api.x.io/orders/43"}
]
}
, {
"links": [
{"rel": ["self"], "href":"http://api.x.io/orders/44"}
]
}
]
, links: [
{rel: ['self'], href: 'http://api.x.io/orders'}
, {rel: ['current'], href: 'http://api.x.io/orders?page=30'}
]
};


var rawCurrentCollection2 = {
entities: [
{
"links": [
{"rel": ["self"], "href":"http://api.x.io/orders/45"}
]
}
, {
"links": [
{"rel": ["self"], "href":"http://api.x.io/orders/46"}
]
}
]
, links: [
{rel: ['self'], href: 'http://api.x.io/orders'}
, {rel: ['current'], href: 'http://api.x.io/orders?page=31'}
]
};

var rawSettingsModel, rawCollection, rawCurrentCollection, rawCurrentCollection2;


beforeEach(function () {
rawSettingsModel = {
"class":["order", "special"]
,"properties":{"orderNumber":42,"itemCount":3,"status":"pending"}
,"entities":[
{"class":["items","collection"],"rel":["http://x.io/rels/order-items", "name:order-items"],"href":"http://api.x.io/orders/42/items"}
,{"class":["info","customer"],"rel":["http://x.io/rels/customer", "name:customer"],"properties":{"customerId":"pj123","name":"Peter Joseph"},"links":[{"rel":["self"],"href":"http://api.x.io/customers/pj123"}]}
]
,"actions":[
{"name":"add-item","title":"Add Item","method":"POST","href":"http://api.x.io/orders/42/items","type":"application/x-www-form-urlencoded","fields":[{name: "addedLater"}, {"name":"orderNumber","type":"hidden","value":"42"},{"name":"productCode","type":"text"},{"name":"quantity","type":"number"}]}]
,"links":[
{"rel":["self"],"href":"http://api.x.io/orders/42"}
,{"rel":["previous"],"href":"http://api.x.io/orders/41"}
,{"rel":["next"],"href":"http://api.x.io/orders/43"}
]
};

rawCollection = {
"class": ["collection"]
, "entities": [
{
"links": [
{"rel": ["self"], "href":"http://api.x.io/orders/41"}
]
}
, {
"links": [
{"rel": ["self"], "href":"http://api.x.io/orders/42"}
]
}
]
, links: [
{"rel": ["self"], "href":"http://api.x.io/orders"}
]
};


rawCurrentCollection = {
entities: [
{
"links": [
{"rel": ["self"], "href":"http://api.x.io/orders/43"}
]
}
, {
"links": [
{"rel": ["self"], "href":"http://api.x.io/orders/44"}
]
}
]
, links: [
{rel: ['self'], href: 'http://api.x.io/orders'}
, {rel: ['current'], href: 'http://api.x.io/orders?page=30'}
]
};


rawCurrentCollection2 = {
entities: [
{
"links": [
{"rel": ["self"], "href":"http://api.x.io/orders/45"}
]
}
, {
"links": [
{"rel": ["self"], "href":"http://api.x.io/orders/46"}
]
}
]
, links: [
{rel: ['self'], href: 'http://api.x.io/orders'}
, {rel: ['current'], href: 'http://api.x.io/orders?page=31'}
]
};
});


describe('.isHydratedObject', function () {
Expand Down Expand Up @@ -227,6 +233,26 @@ describe('Backbone.Siren: ', function () {
});


describe('.parseModel()', function () {
it('creates a new Backbone.Siren.Model from a raw model', function () {
var model = Backbone.Siren.parseModel(rawSettingsModel);
expect(model instanceof Backbone.Siren.Model).toBeTrue();
});


it('updates an existing model if it is already cached', function () {
var store = new Backbone.Siren.Store();
var model1 = Backbone.Siren.parseModel(rawSettingsModel, {store: store});

rawSettingsModel.properties.itemCount = 5;
var model2 = Backbone.Siren.parseModel(rawSettingsModel, {store: store});

expect(model1.get('itemCount')).toBe(5);
expect(model1.cid).toBe(model2.cid);
});
});


describe('.parseCollection()', function () {
it('creates a new collection from a raw collection', function () {
var collection = Backbone.Siren.parseCollection(rawCurrentCollection);
Expand All @@ -238,7 +264,7 @@ describe('Backbone.Siren: ', function () {
var store = new Backbone.Siren.Store();
var collection = new Backbone.Siren.Collection(rawCollection, {store: store});

// We tag the collection so that we know it's the same one
// We tag the collection so that we know it's the same one (unlike models, collections don't have a "cid")
collection.tagged = true;

collection = Backbone.Siren.parseCollection(rawCollection, {store: store});
Expand Down Expand Up @@ -316,6 +342,18 @@ describe('Backbone.Siren: ', function () {
collection = store.get(collection.url());
expect(collection.size()).toBe(2);
});


it('uses existing models from the store instead of creating new ones', function () {
var store = new Backbone.Siren.Store();
var collection = new Backbone.Siren.Collection(rawCollection, {store: store});

// Parse the same raw collection again...
collection = Backbone.Siren.parseCollection(rawCollection, {store: store});

collection = store.get(collection.url());
expect(collection.size()).toBe(2);
});
});


Expand Down

0 comments on commit 5fdfad4

Please sign in to comment.