Javascript Backbonejs如何在更改一个模型时设置集合中所有其他模型的属性

Javascript Backbonejs如何在更改一个模型时设置集合中所有其他模型的属性,javascript,model-view-controller,backbone.js,Javascript,Model View Controller,Backbone.js,我有一个表示产品的模型(在UL中显示为LI项)和一个包含这些产品的集合 当我单击一个LI时,我希望基础模型的属性设置为true,并且集合中的所有其他模型的属性设置为false //Model for product var cs_product = Backbone.Model.extend({ defaults: function(){ return { name: '',

我有一个表示产品的模型(在UL中显示为LI项)和一个包含这些产品的集合

当我单击一个LI时,我希望基础模型的属性设置为true,并且集合中的所有其他模型的属性设置为false

//Model for product
        var cs_product = Backbone.Model.extend({
            defaults: function(){

                return {
                    name: '',
                    active: false
                };

            },
            initialize: function(){

                if(!this.get('name'))
                    this.set({'name': this.defaults.name});

            },
            toggle: function(){


                this.set({
                    active: !this.get('active')
                });

            }
        });





        //Collection of all products this user has access to
        var cs_products = Backbone.Collection.extend({
            _products: [],
            initialize: function(cs_products){
                this._products = cs_products
            },          
            model: cs_product //<-- creates an instance of the cs_product model for each of our products
        });     
        var user_client_products = new cs_products(globals.cs_user.cs_suite_products);
        user_client_products.on('change:active', function(el, i, list){

            console.log('collection change event');
            console.log(arguments);
            console.log(el.toJSON());

            //loop over all models in collection and set active to false except for this?
            this.each(function(el2){

                if(el === el2)
                    return;

                console.log(el2);

                el.set({active: false});


            });


        });
//产品型号
var cs_product=Backbone.Model.extend({
默认值:函数(){
返回{
名称:“”,
活动:错误
};
},
初始化:函数(){
如果(!this.get('name'))
this.set({'name':this.defaults.name});
},
切换:函数(){
这台({
活动:!this.get('active')
});
}
});
//此用户有权访问的所有产品的集合
var cs_products=Backbone.Collection.extend({
_产品:[],
初始化:功能(cs_产品){
这是。_产品=cs_产品
},          

模型:集合中模型上的cs_product/事件还包括:

主干网。收藏
[…]
为方便起见,在集合中的模型上触发的任何事件也将在集合中直接触发。这允许您侦听集合中任何模型中特定属性的更改,例如:
Documents.on(“更改:已选定”,…)

因此,您的集合只需绑定到自身上的
“change:active”
事件,即可侦听其模型中的
“change:active”
事件。然后,它可以将自身中的其余模型设置为非活动:

var cs_products = Backbone.Collection.extend({
    model: cs_product,
    initialize: function() {
        _.bindAll(this, 'propagate_active');
        this.on('change:active', this.propagate_active);
    },
    propagate_active: function(p) {
        if(!p.get('active'))
            return;
        this.each(function(m) {
            if(p.id != m.id)
                m.set({ active: false }, { silent: true });
        });
    }
});
演示:


顺便说一句,没有理由自己跟踪某个系列中的模型:

var cs_products = Backbone.Collection.extend({
    _products: [],
    initialize: function(cs_products) {
        this._products = cs_products; // <------------- ????
    },
    //...
});
var cs_products=Backbone.Collection.extend({
_产品:[],
初始化:功能(cs_产品){
这是。_产品=cs_产品//