documentcloud/backbone — Issue #988: hasChanged(): deep attribute comparison.
Author: Westbrook
Created: 2012-02-10 22:26:34+00:00
State: open

I often find myself with attributes that aren't at the first level of the JSON that I'd like to checked their hasChanged() state, but currently this function only supports checking on the first level of the object. I used a little hack for myself like so:

hasChanged:function(attr){
if(attr){
if(attr.indexOf('.')==-1){
return this._previousAttributes[attr]!=this.attributes[attr]
}else if(attr=attr.split('.')){
var a, b;
if(this._previousAttributes[attr[0]]){
a = this._previousAttributes[attr[0]];
b = this.attributes[attr[0]];
for (var i=1,c=attr.length;i