推荐使用jQuery UI Widget Factory删除destroy上的事件的方法 [英] Recommended way to remove events on destroy with jQuery UI Widget Factory
问题描述
我正在使用jQuery UI Widget Factory来构建一个jQuery插件。
I'm using the jQuery UI Widget Factory to build a jQuery plugin.
我的插件将自定义事件绑定到窗口...
My plugin binds custom events to the window...
_subscribe: function() {
$(window).on("dragger.started", function() { ... });
}
我想知道如何删除这些事件,当一个特定的实例插件被破坏了。如果我使用...
I am wondering how to go about removing these events, when a particular instance of the plugin is destroyed. If I use...
destroy: function() {
$(window).off("dragger.started");
}
...那么这将搞乱插件的任何其他实例页面,因为它将删除所有dragger.started事件。
...then that will mess up any other instances of the plugin on the page, as it will remove all "dragger.started" events.
建议的方法是仅销毁与插件实例关联的那些事件?
What is the recommended way to go about destroying only those events that are associated with an instance of the plugin?
感谢(提前)求助。
推荐答案
您可以在事件中绑定多个名称空间。因此,为您的实例分配一个ID,如
You can bind multiple namespaces in an event. So assign your instance an id like
//this.id = 'dragger_' + guid_or_static_count
$(window).on("dragger.started." + this.id, function() { ... });
以后
$(window).off('dragger.' + this.id);
请参阅小提琴
这篇关于推荐使用jQuery UI Widget Factory删除destroy上的事件的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!