私は多くの js スクリプトと jQuery プラグインを書いてきたので、ここに私のジレンマの要約された疑似コード シナリオを示します。
プラグインに渡すメソッド オブジェクトを定義する適切に作成された jQuery プラグインがあるとします。
// OUR HYPOTHETICAL jQUERY PLUGIN
(function($){
var methods = {
init : function(options){
var setup = $.extend({
'speed' : 300,
'msg' : 'blah'
}, options)
return this.each(function(){
var animation = setInterval(function(){ ... },1000)
...
})
},
stop : function(){
window.clearInterval(animation); // DOES NOT WORK
}
};
$.fn.somePlugin = function(method){
// Method calling logic
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( 'Bad Method...' );
}
};
})(jQuery)
このプラグインを div にインスタンス化することを想像してみましょう$('#someDiv').somePlugin();
この時点までのすべてが正常に機能し、電話をかけることができます...
$('#someDiv').somePlugin('stop');
ただし、stopメソッドの内部では、プラグインの初期化中に作成されたアニメーション プロパティのインスタンスにアクセスする方法を理解できません。
#someDivにバインドされたプラグインに関連付けられているアニメーション変数の特定のインスタンスを取得する方法に関するアイデア。