From 30a1dbb30e32dbea026a6efab7b87a71d775bfd2 Mon Sep 17 00:00:00 2001 From: Matt DeGennaro Date: Mon, 5 Dec 2016 16:12:56 -0500 Subject: [PATCH 1/3] Adding passing test that hands a constructed Collection to a relation property --- test/adaptors/backbone/base_model_spec.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/adaptors/backbone/base_model_spec.js b/test/adaptors/backbone/base_model_spec.js index 902ef8a0..4bfd76ea 100644 --- a/test/adaptors/backbone/base_model_spec.js +++ b/test/adaptors/backbone/base_model_spec.js @@ -180,6 +180,16 @@ 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'); }); }); describe('trigger', function() { From 93913f067d9d57895fcfe7d9a7d64c609a4f3843 Mon Sep 17 00:00:00 2001 From: Matt DeGennaro Date: Mon, 5 Dec 2016 16:14:23 -0500 Subject: [PATCH 2/3] Adding equivalent test that passes a Model to a relation property, this one failing --- test/adaptors/backbone/base_model_spec.js | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/adaptors/backbone/base_model_spec.js b/test/adaptors/backbone/base_model_spec.js index 4bfd76ea..83767466 100644 --- a/test/adaptors/backbone/base_model_spec.js +++ b/test/adaptors/backbone/base_model_spec.js @@ -191,6 +191,30 @@ 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('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() { it('should be a function', function() { From de25e24a21e9e8232f8c4a98cd1b32b906438f13 Mon Sep 17 00:00:00 2001 From: Matt DeGennaro Date: Mon, 5 Dec 2016 16:21:02 -0500 Subject: [PATCH 3/3] Proposed fix to serialize passed models as collections are doing --- adaptors/backbone/base_model.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adaptors/backbone/base_model.js b/adaptors/backbone/base_model.js index b9865e32..f95fc2b7 100644 --- a/adaptors/backbone/base_model.js +++ b/adaptors/backbone/base_model.js @@ -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; }