Recommended way to remove events on destroy with jQuery UI Widget Factory

。_饼干妹妹 提交于 2019-12-11 02:35:58

问题


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");
}

...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?

Thanks (in advance) for your help.


回答1:


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() { ... });

And later

$(window).off('dragger.' + this.id);

See this fiddle




回答2:


What are you using to get rid of the instance? jQuery remove() will remove bound events, I believe.



来源:https://stackoverflow.com/questions/13907368/recommended-way-to-remove-events-on-destroy-with-jquery-ui-widget-factory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!