Have view listen to collection event

前端 未结 3 1818
情话喂你
情话喂你 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:04

    You have to bind your view to listen on the "add" event of your collection:

    var MyView = Backbone.View.extend({
        initialize: function(){
            this.collection.bind('add', this.somethingWasAdded, this)
        },
        somethingWasAdded: function(){
    
        }
    });
    new MyView({collection: myCollection})
    

提交回复
热议问题