看看previousAttributes()
然后你可以比较:
If(this.get(attr) > this.previousAttributes()[attr]){
console.log('bigger');
} else {
console.log('smaller');
}
如果你在你的change
事件处理程序中使用它,你就准备好了。无需自定义触发器或大量代码。
编辑
这来自我的Backbone.Validators项目以及我如何获取在验证步骤期间更改的所有属性的列表:
var get_changed_attributes = function(previous, current){
var changedAttributes = [];
_(current).each(function(val, key){
if(!_(previous).has(key)){
changedAttributes.push(key);
} else if (!_.isEqual(val, previous[key])){
changedAttributes.push(key);
}
});
return changedAttributes;
};
这需要下划线 1.3.1,因为它使用_.has
. 如果您无法升级,那很容易更换。在你的情况下,你会通过this.previousAttributes()
并且this.attributes