假设我有一个状态
的骨干模型var myModel = new Backbone.Model(
{
key1:'value1',
key2:'value2',
key3:'value3'
});
myModel.on('all', function allHanlder () {
console.log(arguments);
})
我需要删除key1和key2并更改key3。可能的选项是
选项1导致3次更改,3次更改属性事件
myModel.unset('key1');
myModel.unset('key2');
myModel.set({key3:'newValue3'})
//events
["change:key1", Object { cid="c1355", attributes={...}, _changing=true, more...}, undefined, Object { unset=true}]
["change", Object { cid="c1355", attributes={...}, _changing=true, more...}, Object { unset=true}]
["change:key2", Object { cid="c1355", attributes={...}, _changing=true, more...}, undefined, Object { unset=true}]
["change", Object { cid="c1355", attributes={...}, _changing=true, more...}, Object { unset=true}]
["change:key3", Object { cid="c1355", attributes={...}, _changing=true, more...}, "newValue3", Object {}]
["change", Object { cid="c1355", attributes={...}, _changing=true, more...}, Object {}]
选项2将导致2个更改更改:属性事件
myModel.clear()
myModel.set({key3:'newValue3'})
//output
["change:key1", Object { cid="c1356", attributes={...}, _changing=true, more...}, undefined, Object { unset=true}]
["change:key2", Object { cid="c1356", attributes={...}, _changing=true, more...}, undefined, Object { unset=true}]
["change:key3", Object { cid="c1356", attributes={...}, _changing=true, more...}, undefined, Object { unset=true}]
["change", Object { cid="c1356", attributes={...}, _changing=true, more...}, Object { unset=true}]
["change:key3", Object { cid="c1356", attributes={...}, _changing=true, more...}, "newValue3", Object {}]
["change", Object { cid="c1356", attributes={...}, _changing=true, more...}, Object {}]
更改:属性事件是公平的,但我需要最小化更改触发器。像下面的东西。
myModel.someMagicSet({key3:'newValue3'})
//output
["change:key1", Object { cid="c1355", attributes={...}, _changing=true, more...}, undefined, Object {}]
["change:key2", Object { cid="c1355", attributes={...}, _changing=true, more...}, undefined, Object {}]
["change:key3", Object { cid="c1355", attributes={...}, _changing=true, more...}, "newValue3", Object {}]
["change", Object { cid="c1355", attributes={...}, _changing=true, more...}, Object {}]
我是否有办法在不超越骨干行为的情况下实现它?
答案 0 :(得分:1)
myModel.unset('key1', {silent: true});
myModel.unset('key2', {silent: true});
myModel.set({key3:'newValue3'}, {removed: ['key1', 'key2']});
只需触发一个更改事件,这将执行您想要的操作。您将已删除的项目传递给options对象中的处理程序。在您的处理程序中,您的代码应如下所示:
myModel.on('all', function allHandler (model, options) {
console.log(options.removed); // this should show you what has been removed before
// other code
})
或者,您可以在选项中传递一个标记,您可以在处理程序中分析每个事件,这将确定处理程序是否已运行。这将使您的处理程序可以访问已更改的内容,但是它必须将其存储在其自己的范围之外,以便下次访问它。
myModel.unset('key1', {finished: false});
myModel.unset('key2', {finished: false});
myModel.set({key3:'newValue3'}, {finished: true});
编辑:这是一个应该做的所有事情:
myModel.clear({reset: {key3: 'newValue3'}});
然后在你的处理程序中执行此操作
myModel.on('all', function allHandler (model, options) {
if (options.reset){
model.set(options.reset);
}
// other code
})
你应该注意的是clear
也删除了id,所以如果你需要,你应该将它传递给你:
myModel.clear({reset: {key3: 'newValue3', id: myModel.get('id')}});