Last active
August 29, 2015 14:14
-
-
Save kkotaro0111/fe844eb944e8a1288a5e to your computer and use it in GitHub Desktop.
jQueryプラグインを作るときのテンプレート。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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