为什么Backbone事件替换html不起作用? [英] Why Backbone events on replacing html does not work?

查看:127
本文介绍了为什么Backbone事件替换html不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将视图存储在 window.myView 变量中,则将其渲染,然后调用javascript控制台:

If I store view in window.myView variable, render it, then call in javascript console:

$('#container').html('')

然后调用:

$('#container').html(window.myView.$el)

绑定事件将停止工作。

我很漂亮确实应该是这样,但是:

I'm pretty sure that is supposed to be so, but:


  • 为什么它的工作原理?

  • 为什么调用 myView.render()不会丢失,如何重新呈现子视图w / o丢失事件绑定?

  • <事件绑定?
  • why exactly it works this way?
  • how to re-render subpart of view w/o losing event bindings?
  • why calling myView.render() won't lose event bindings?

更新:

找到这个

Found this article. Is that the reason?


确保jQuery没有卸载你不想要的事件

Make sure jQuery isn’t unloading your events when you don’t want it to

如果您正在建立一个即时创建视图的应用程序,并将其附加/删除到dom,那么您可能会遇到问题。每次从dom中删除视图时,jQuery将卸载所有事件。所以你不能引用一个视图,并从dom中删除它,然后再重新附加它。所有的事件都将被卸载。如果你想保持观点,一个更好的主意是使用display:none来隐藏它们。但是,您不应该滥用此回收视图,您将不会使用一段时间(并防止内存泄漏)。

If you are building an app where you create views on the fly and attach/remove them to the dom, you may have a problem. Everytime you remove a view from the dom, jQuery unloads all the events. So you can’t have a reference to a view and remove it from the dom and then re-attach it later. All your events would have been unloaded. If you wanna keep the views around, a better idea is to hide them using display:none. However, you should not abuse this and recycle views that you are not going to use for a while (and prevent memory leaks).


推荐答案

jQuery html code>事件正在清理所有jquery事件和数据绑定,以防止内存泄漏(您可以检查jQuery源代码 cleanData 方法来找出更多 - 这是一个没有文档的方法)

jQuery empty, html and remove events are cleaning up all the jquery event and data bindings to prevent memory leaks (you can check jQuery source code for cleanData method to figure out more - it's an undocumented method)

view.render()不会删除事件,因为Backbone视图事件是使用事件绑定的并且将视图的 el 而不是直接转到视图中的元素。

view.render() doesn't remove the events because Backbone view events are bound using event delegation and are bound to the view's el rather then directly to the elements in the view.

如果你想重新使用您的视图,您可以使用jQuery detach 方法来删除它们,该方法可以保持所有事件和数据的约束,但必须注意不要以这种方式产生内存泄漏。 ( jquery分离文档

If you want to reuse your views you can remove them using the jQuery detach method which keeps all the events and data bound though you have to watch out not to produce memory leaks this way. (jquery detach docs)

如果你想要走第一种方式,您可以随时使用Backbone.View delegateEvents 方法重新绑定Backbone事件。 (主干文档

If you want to go the first way you can always rebind the Backbone events easily using the Backbone.View delegateEvents method. (backbone doc)

ps。使用jQuery .empty()而不是 .html('')作为jQuery html也更干净,更优化方法始终调用空,首先在插入新的html之前先清除所有的事件和数据。也不会混合jquery和本机DOM innerHTML,因为它可能会产生内存泄漏,因为没有清理jQuery事件/数据

ps. it's also cleaner and more optimal to use jQuery .empty() rather then .html('') as jQuery html method always calls empty first to clean up all the events and data first before it inserts new html. Also never mix jquery and native DOM innerHTML as it might produce memory leaks cause of not cleaned up jQuery events/data

这篇关于为什么Backbone事件替换html不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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