Last active
December 21, 2015 15:28
-
-
Save thomasyip/6326560 to your computer and use it in GitHub Desktop.
Skeleton of jQuery plugin
This file contains 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($, undefined) { | |
var PLUGIN_NAME = 'plugin'; | |
var DEFAULT_OPTIONS = { | |
optionA: true, | |
optionB: false, | |
'callbackC': function() { | |
} | |
}; | |
function plugin(options) { | |
var element = this; | |
var $element = $(element); | |
var instance = {}; | |
var options = $.extend({}, DEFAULT_OPTIONS, options); | |
// main action here: | |
// ... | |
instance = $.extend(instance, { | |
// public methods: | |
}); | |
return instance; | |
} | |
$.fn[PLUGIN_NAME] = function(method, arg) { | |
var args = arguments, | |
results = undefined, $selector; | |
$selector = $(this).each(function() { | |
var $this = $(this), | |
instance; | |
instance = $this.data(PLUGIN_NAME); | |
if (instance === undefined) { | |
if (typeof method !== 'string') { | |
$this.data(PLUGIN_NAME); | |
instance = plugin.call(this, method); | |
$this.data(PLUGIN_NAME, instance); | |
} else { | |
$.error('Function "' + method + '" is called before "' + PLUGIN_NAME + '" is initialized.'); | |
} | |
} else { | |
if (method in instance && $.isFunction(instance[method])) { | |
results = instance[method].apply(instance, Array.prototype.slice.call(args, 1)); | |
} else { | |
$.error('Function "' + method + '" is not found in "' + PLUGIN_NAME + '".'); | |
} | |
} | |
}); | |
return results !== undefined? results: $selector; | |
}; | |
}($)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Optional css file loading: