Skip to content

Commit

Permalink
Make changedAttributes return nested values
Browse files Browse the repository at this point in the history
This is mostly there. There's a problem with clear() right now.
  • Loading branch information
gkatsev committed Jan 6, 2014
1 parent b02052a commit b1a8a04
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
10 changes: 9 additions & 1 deletion backbone-nested.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
(function(){
'use strict';

var _delayedTriggers = [];
var _delayedTriggers = [],
nestedChanges;

Backbone.NestedModel = Backbone.Model.extend({

Expand Down Expand Up @@ -65,6 +66,8 @@
}
}

nestedChanges = this.changed;

if (opts.unset && attrPath && attrPath.length === 1){ // assume it is a singular attribute being unset
// unsetting top-level attribute
unsetObj = {};
Expand Down Expand Up @@ -183,6 +186,11 @@
return this;
},

changedAttributes: function() {
var backboneChanged = Backbone.NestedModel.__super__.changedAttributes.call(this);
return _.extend({}, nestedChanges, backboneChanged);
},

toJSON: function(){
return Backbone.NestedModel.deepClone(this.attributes);
},
Expand Down
20 changes: 16 additions & 4 deletions test/nested-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,12 @@ $(document).ready(function() {
initial: 'L',
full: 'Limburger'
}
}
},
'name.middle': {
initial: 'L',
full: 'Limburger'
},
'name.middle.full': 'Limburger'
});
});

Expand All @@ -726,7 +731,12 @@ $(document).ready(function() {
initial: 'L',
full: 'Limburger'
}
}
},
'name.middle': {
initial: 'L',
full: 'Limburger'
},
'name.middle.full': 'Limburger'
});
});

Expand Down Expand Up @@ -766,7 +776,8 @@ $(document).ready(function() {
initial: 'L',
full: 'Lee'
}
}
},
'name.last': 'Dylan'
});
});

Expand All @@ -791,7 +802,8 @@ $(document).ready(function() {
initial: 'L',
full: 'Lee'
}
}
},
'name.last': 'Dylan'
});
});

Expand Down

0 comments on commit b1a8a04

Please sign in to comment.