Backbone集合布尔翻转

时间:2014-02-12 05:45:19

标签: backbone.js backbone-events backbone-collections

当我在集合中监听模型的布尔属性的更改事件时,如何处理事件以切换以在集合中的所有其他模型上关闭bool。

1 个答案:

答案 0 :(得分:0)

试试这个:

// in your model
yourModel.on('change:yourBooleanAttribute', yourCollection.setAllButCurrent);

// and in your collection
setAllButCurrent: function(changedModel, value, options) {
    // here you can iterate through `this.models` and for each model 
    // if it's `id` differ from changedModel id, change it's `yourBooleanAttribute`
    // to the opposite value of `value`
}