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(
(attrs = {})[key] = val;
}
if (options && options.merge) {
nestedOptions = {silent: false, merge: true};
nestedOptions = {silent: false, merge: true, parse: options.parse};
for (attr in attrs) {
curVal = this.get(attr);
newVal = attrs[attr];
if (curVal instanceof Backbone.Model && newVal instanceof Backbone.Model) {
delete attrs[attr];
curVal.set(newVal.attributes, nestedOptions);
if (curVal instanceof Backbone.Model) {
if (newVal instanceof Backbone.Model) {
delete attrs[attr];
curVal.set(newVal.attributes, nestedOptions);
} else if (options.parse && typeof newVal === 'object') {
delete attrs[attr];
curVal.set(newVal, nestedOptions);
}
}
}
}
......
......@@ -189,6 +189,33 @@ define(function(require) {
} else {
expect(top.counts).toBeUndefined();
}
result = top.set('nested', {address1: '1234 Main St.'}, {merge: true, parse: true});
expect(result).toBe(top);
var cidsSetnestedpojoparse = extractCids(top);
expect(cidsSetnestedmerge.top).toEqual(cidsSetnestedpojoparse.top);
expect(cidsSetnestedmerge.nested).toEqual(cidsSetnestedpojoparse.nested);
expect(top.get('name')).toEqual('value');
expect(top.get('nested').get('address1')).toEqual('1234 Main St.');
if (checkCounts) {
expect(top.counts).toEqual({set: 7});
} else {
expect(top.counts).toBeUndefined();
}
result = top.set('nested', {address1: '4321 Main St.'}, {merge: true});
expect(result).toBe(top);
var cidsSetnestedpojo = extractCids(top);
expect(cidsSetnestedpojoparse.top).toEqual(cidsSetnestedpojo.top);
expect(cidsSetnestedpojo.nested).toBeUndefined();
expect(top.get('name')).toEqual('value');
expect(top.get('nested')).not.toEqual(jasmine.any(Backbone.Model));
expect(top.get('nested').address1).toEqual('4321 Main St.');
if (checkCounts) {
expect(top.counts).toEqual({set: 8});
} else {
expect(top.counts).toBeUndefined();
}
}
it('set-mixin-plain', function() {
testSet.call(this, NestedModels.mixin(Top), false);
......