一旦视图不再需要undelegate事件的最好办法 [英] The best way to undelegate events once a view is no longer needed

查看:236
本文介绍了一旦视图不再需要undelegate事件的最好办法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是个不好的做法叫 undelegateEvents()视图删除()的方法?为什么不是默认包含由骨干家伙?

我意识到我陷入如此多的约束问题,当简单地重新初始化一个视​​图变量。虽然 undelegateEvents()被自动调用创建一个新的视图时,它试图undelegate事件新实例化的视图,而不是previous之一。因此,除非手动每次调用它,鬼事件回调仍然活着,搞砸了我的应用程序。

什么是处理这个问题的最好方法?


解决方案

  

这是个不好的做法叫 undelegateEvents()视图删除()方法?


它,除非你实现,没有必要自己删除()键,你不叫 Backbone.View.remove()这一点。$ el.remove()。那是,如果你使用jQuery,至少。调用删除()中的骨干视图将调用 jQuery.remove()这将删除所有的DOM事件监听器反正。


  

我意识到我陷入如此多的约束问题,当简单地重新初始化一个视​​图变量。


很多人似乎使用Backbone.Events喜欢它的某种魔力,他们并不需要经过清理,例如:

  VAR查看= Backbone.View.extend({  初始化:功能(选件){    //`在()`或`bind()的`    this.model.on('东西',this.render,这一点);  }});

请参阅我的<一个答案href=\"http://stackoverflow.com/questions/10485610/delegating-events-to-a-parent-view-in-backbone/10486571#10486571\">Delegating事件中的骨干父视图

有没有可能是你所遇到的鬼事件问题与骨干事件,而不是DOM事件?

如果您保持模型对象周围,但要摆脱视图对象或其骨干事件注册的,你要做的 view.model.off(NULL,NULL,这一点); 。你必须解除你已经注册的任何外部对象的事件。如果你愿意,你可以覆盖 Backbone.View.remove(),做它在那里,但默认情况下该方法仅仅是视图的简写。$ el.remove()

Is it a bad practice to call undelegateEvents() in the view remove() method? Why wasn't it included by default by the backbone guys?

I realized I am falling into so many binding issues, when simply reinitializing a view variable. Although undelegateEvents() is being called automatically when a new view is created, it is trying to undelegate events for the newly instantiated view, not the previous one. Therefore, unless manually calling it every time, ghost event callbacks remain alive and screw up my apps.

What's the best way to handle this?

解决方案

Is it a bad practice to call undelegateEvents() in the view remove() method?

It's not necessary unless you're implementing your own remove() and you don't call Backbone.View.remove() or this.$el.remove(). That's if you're using jQuery, at least. Calling remove() on a Backbone view will call jQuery.remove() which will remove all of the DOM event listeners anyway.

I realized I am falling into so many binding issues, when simply reinitializing a view variable.

A lot of people seem to use Backbone.Events like it's some kind of magic that they don't need to clean up after, e.g.:

var View = Backbone.View.extend( {

  initialize : function ( options ) {

    // `on()` or `bind()`

    this.model.on( 'something', this.render, this );

  }

} );

See my answer on Delegating events to a parent view in Backbone

Is it possible that the ghost event issues you're experiencing are with Backbone events and not DOM events?

If you keep the model object around but want to get rid of that view object or its Backbone event registrations, you have to do view.model.off( null, null, this );. You have to unbind the events that you've registered on any external objects. If you want, you could override Backbone.View.remove() and do it there, but by default that method is just shorthand for view.$el.remove().

这篇关于一旦视图不再需要undelegate事件的最好办法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆