Have view listen to collection event

前端 未结 3 1856
情话喂你
情话喂你 2021-02-04 12:53

I have a view myView and a collection myCollection. When I add a model to myCollection, the add event is trigger

3条回答
  •  花落未央
    2021-02-04 13:11

    After ver. 0.9.9 (added Dec. 13, 2012) it is recommended to use listenTO.

    In line with this:

    var MyView = Backbone.View.extend({
    
        initialize: function() {
            this.listenTo(this.collection, 'add', this.onModelAdd);
        },
        onModelAdd: function(model) {
            // do something
        }
    });
    
    var myCollection = new MyCollection();
    var myView = new MyView({collection: myCollection});
    

提交回复
热议问题