1

ぼかしイベントのウィンドウをリッスンする jQuery プラグインを作成しました。

プラグインのインスタンスを破棄するときにリスナーをオフにできるように、プラグイン自体の内部に一意の ID を作成したいと考えています。これらの uniqueIds はどのように作成すればよいですか?

以下の例は明らかに機能しません。destroy メソッドの incrementId は、常に最後のプラグイン インスタンスからブラーを削除しています。

 (function( $ ) {

    var incrementId = 0;

    var methods = 
    {
        init : function( options ) {
            var that = this;
            incrementId += 1;
            $(window).on( "blur.pleaseKillMe" + incrementId, function( e ) {
                that.css( "color", "red" );
            });
        },

        destroy : function( ) {
            console.log( "and... " + incrementId );
            $(window).off( "blur.pleaseKillMe" + incrementId );
        }
    };

    $.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 );
4

1 に答える 1

1
incrementId += 1;
this.data('id', incrementId);
...
$(window).off('blur.pleaseKillMe' + this.data('id');
于 2013-01-26T02:20:44.880 に答える