88d84fee by Adam Heath

Issue #2608: Add support for parsing incoming pojo into an existing

model child instance.
1 parent 3a21b2ec
...@@ -83,13 +83,18 @@ define( ...@@ -83,13 +83,18 @@ define(
83 (attrs = {})[key] = val; 83 (attrs = {})[key] = val;
84 } 84 }
85 if (options && options.merge) { 85 if (options && options.merge) {
86 nestedOptions = {silent: false, merge: true}; 86 nestedOptions = {silent: false, merge: true, parse: options.parse};
87 for (attr in attrs) { 87 for (attr in attrs) {
88 curVal = this.get(attr); 88 curVal = this.get(attr);
89 newVal = attrs[attr]; 89 newVal = attrs[attr];
90 if (curVal instanceof Backbone.Model && newVal instanceof Backbone.Model) { 90 if (curVal instanceof Backbone.Model) {
91 if (newVal instanceof Backbone.Model) {
91 delete attrs[attr]; 92 delete attrs[attr];
92 curVal.set(newVal.attributes, nestedOptions); 93 curVal.set(newVal.attributes, nestedOptions);
94 } else if (options.parse && typeof newVal === 'object') {
95 delete attrs[attr];
96 curVal.set(newVal, nestedOptions);
97 }
93 } 98 }
94 } 99 }
95 } 100 }
......
...@@ -189,6 +189,33 @@ define(function(require) { ...@@ -189,6 +189,33 @@ define(function(require) {
189 } else { 189 } else {
190 expect(top.counts).toBeUndefined(); 190 expect(top.counts).toBeUndefined();
191 } 191 }
192
193 result = top.set('nested', {address1: '1234 Main St.'}, {merge: true, parse: true});
194 expect(result).toBe(top);
195 var cidsSetnestedpojoparse = extractCids(top);
196 expect(cidsSetnestedmerge.top).toEqual(cidsSetnestedpojoparse.top);
197 expect(cidsSetnestedmerge.nested).toEqual(cidsSetnestedpojoparse.nested);
198 expect(top.get('name')).toEqual('value');
199 expect(top.get('nested').get('address1')).toEqual('1234 Main St.');
200 if (checkCounts) {
201 expect(top.counts).toEqual({set: 7});
202 } else {
203 expect(top.counts).toBeUndefined();
204 }
205
206 result = top.set('nested', {address1: '4321 Main St.'}, {merge: true});
207 expect(result).toBe(top);
208 var cidsSetnestedpojo = extractCids(top);
209 expect(cidsSetnestedpojoparse.top).toEqual(cidsSetnestedpojo.top);
210 expect(cidsSetnestedpojo.nested).toBeUndefined();
211 expect(top.get('name')).toEqual('value');
212 expect(top.get('nested')).not.toEqual(jasmine.any(Backbone.Model));
213 expect(top.get('nested').address1).toEqual('4321 Main St.');
214 if (checkCounts) {
215 expect(top.counts).toEqual({set: 8});
216 } else {
217 expect(top.counts).toBeUndefined();
218 }
192 } 219 }
193 it('set-mixin-plain', function() { 220 it('set-mixin-plain', function() {
194 testSet.call(this, NestedModels.mixin(Top), false); 221 testSet.call(this, NestedModels.mixin(Top), false);
......