Backbone.js: how to unbind from events, on model remove

后端 未结 3 1978
执念已碎
执念已碎 2020-12-15 12:42

in backbone we have an app that uses an event Aggregator, located on the window.App.Events now, in many views, we bind to that aggregator, and i manually wrote

3条回答
  •  误落风尘
    2020-12-15 13:23

    I think the clean references process is a tricky part of Backbone.

    When you remove a Model from a Collection the Collection takes care to unbind any event on the Model that the Collection its self is binding. Check this private Collection method.

    Maybe you can use such a same technique in your Aggregator:

    // ... Aggregator code
    the_model.on( "remove", this.unlinkModel, this );
    // ... more Aggregator code
    
    unlinkModel: function( model ){
      model.off( null, null, this );
    }
    

    This is in the case the direction of the binding is Aggregator -> Model. If the direction is the opposite I don't think you have to make any cleaning after Model removed.

提交回复
热议问题