私はいくつかのクリックイベントがあるWebアプリケーションページを持っていますが、私は以下に示すjQueryプラグインパターンを適用しようとしています。Jqueryプラグインのパターンどこにクリックハンドラを配置するか
; (function ($, window, document, undefined) {
var Plugin = function (elem, options) {
this.elem = elem;
this.$elem = $(elem);
this.options = options;
};
Plugin.prototype = {
defaults: {
message: 'This is message'
},
init: function() {
this.config = $.extend({}, this.defaults, this.options);
var self = this;
this.selected = $('#selector');
// IS THIS THE BEST PLACE TO PUT .click EVENT HANDLERS ??
this.selected.on('click', function() {
console.log('this.selected clicked');
self.sampleMethod();
});
this.sampleMethod();
return this;
},
sampleMethod: function() {
console.log('sampleMethod');
}
};
Plugin.defaults = Plugin.prototype.defaults;
$.fn.testPlugin = function (options) {
return this.each(function() {
new Plugin(this, options).init();
});
};
})(jQuery, window, document);
私の質問は:私は、init関数内でクリックイベントハンドラを配置した(と彼らが働く)が、これはそれらを置くための正しいかつ堅牢な場所ですか?
これに関するお手伝いがあれば幸いです。