How to use Backbone.EventBinder with views

Referring to this post on Backbone.EventBinder, I get lost in how to use EventBinder with Backbone views (this is the most popular use case). It is still recommended that you add the close () method to the Backbone.View prototype and the onClose () method for the view, as suggested in this post? Also, where is the middleware object stored, so that binder.unbindAll () can be called on close? What is the recommended way to close child views (for example, a parent view in a collection that has child views on related models). The working example will be a great addition to the Backbone.EventBinder project.

+4
source share
1 answer

Yes, you must add the close method to your views. EventBinder does not deny what this Zombies post says. Rather, it helps automate many processes, making it easier to disable all of your events in the view.

Take a look at the source code of Marionette.View for an example of how it is used:

https://github.com/marionettejs/backbone.marionette/blob/master/src/marionette.view.js#L9 https://github.com/marionettejs/backbone.marionette/blob/master/src/marionette.view .js # L16 https://github.com/marionettejs/backbone.marionette/blob/master/src/marionette.view.js#L97

If you are using Marionette, you do not need to add the close method yourself or add a mediation event yourself. That was for you.

If you want to add this to your own views, it's easy:

 MyView = Backbone.View.extend({ initialize: function(){ // add the event binder this.eventBinder = new Backbone.EventBinder(); // bind some stuff this.eventBinder.bindTo(this.model, "change:foo", this.doStuff, this); }, close: function(){ // ... other stuff this.eventBinder.unbindAll(); } }); 
+2
source

All Articles