我需要一些关于如何在插件被破坏时清理它的设计建议。
我在窗口上监听blur
事件,但不确定如何在插件被销毁时删除这些事件。如果插件应用于多个元素,但只为一个元素销毁,它应该仍然适用于其他元素。设计这个的正确方法是什么?
(function( $ ) {
var methods =
{
init : function( options ) {
var that = this;
$(window).blur( function( e ) {
that.css( "color", "red" );
console.log( "still here" );
});
},
destroy : function( ) {
console.log( "hmmm, got to take out that blur" );
}
};
$.fn.pleaseKillMe = function( method )
{
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
}
else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
}
else {
$.error( 'Method ' + method + ' does not exist on jQuery.p5shrinkwrap' );
}
};
})( jQuery );