Skip to content

Instantly share code, notes, and snippets.

@kkotaro0111
Last active August 29, 2015 14:14
Show Gist options
  • Save kkotaro0111/fe844eb944e8a1288a5e to your computer and use it in GitHub Desktop.
Save kkotaro0111/fe844eb944e8a1288a5e to your computer and use it in GitHub Desktop.
jQueryプラグインを作るときのテンプレート。
(function($){
var Plugin = function($t, settings){
this.$el = $t;
this.settings = settings;
this.init();
};
Plugin.prototype = {
init: function(){
console.log("init", this, arguments);
},
standby: function(){
console.log("standby", this, arguments);
},
start: function(){
console.log("start", this, arguments);
},
stop: function(){
console.log("stop", this, arguments);
},
reset: function(){
console.log("reset", this, arguments);
}
};
$.fn.plugin = function(options){
if( typeof options == "string" ){
var args = [];
args.push.apply(args, arguments);
return this.each( function(){
var $t = $(this);
var cls = $t.data("plugin");
if(cls && cls[options]){
args.splice(0, 1);
cls[options](cls, args);
}
});
}else{
var settings = $.extend({
}, options );
return this.each( function(){
var $t = $(this);
var cls = $t.data("plugin");
if( typeof cls === "undefined"){
cls = new Plugin($t, settings);
$t.data("plugin", cls);
}
});
}
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment