Skip to content
This repository has been archived by the owner on Mar 1, 2019. It is now read-only.

Setting a model relation with a model #280

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion adaptors/backbone/base_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ BaseModel.prototype.setRelation = function(attr, val, options) {
}

if (relation && relation instanceof Backbone.Model) {
relation.set(val);
relation.set(val && val.toJSON ? val.toJSON() : val);
return relation;
}

Expand Down
34 changes: 34 additions & 0 deletions test/adaptors/backbone/base_model_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,40 @@ describe('base_model.js constructed api', function() {
});
expect(testModel.get('collection').pluck('id').join('&')).to.equal('2&3&4');
expect(testModel.get('collection').pluck('n').join('&')).to.equal('1&2&3');

testModel.set({
collection: new BaseCollection([
{ id: 2, n: 2 },
{ id: 3, n: 3 },
{ id: 4, n: 4 }
])
});
expect(testModel.get('collection').pluck('id').join('&')).to.equal('2&3&4');
expect(testModel.get('collection').pluck('n').join('&')).to.equal('2&3&4');
});
it('should update a nested model', function() {
var TestModel = BaseModel.extend({
relations: {
model: BaseModel
}
});
var testModel = new TestModel({
model: { id: 1, n: 1 }
});
expect(testModel.get('model')).to.be.instanceof(BaseModel);
expect(testModel.get('model').toJSON()).to.deep.equal({ id: 1, n: 1 });

testModel.set({
model: { id: 2 }
});
expect(testModel.get('model')).to.be.instanceof(BaseModel);
expect(testModel.get('model').toJSON()).to.deep.equal({ id: 2, n: 1 });

testModel.set({
model: new BaseModel({ n: 2 })
});
expect(testModel.get('model')).to.be.instanceof(BaseModel);
expect(testModel.get('model').toJSON()).to.deep.equal({ id: 2, n: 2 });
});
});
describe('trigger', function() {
Expand Down