Last active
December 6, 2016 11:35
-
-
Save angel-vladov/723e11f9d1b58b1962ccc90d7d2ec023 to your computer and use it in GitHub Desktop.
jQuery prototypical plugin example
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
// globals jQuery | |
(function ($) { | |
'use strict'; | |
// TODO: Replace pluginExample and PluginExample with your plugin name | |
$.fn.pluginExample = function (option/*, varArgs*/) { | |
var args = Array.prototype.slice.call(arguments, 1); | |
return this.each(function () { | |
var $element = $(this); | |
var ctrl = $element.data('pluginExample'); | |
if (!ctrl) { | |
var opts = $.extend({}, | |
$.fn.pluginExample.defaults, | |
typeof option == 'object' && option | |
); | |
$element.data('pluginExample', (ctrl = new PluginExample(this, opts))); | |
} | |
if (typeof option == 'string') { | |
ctrl[option].apply(ctrl, args); | |
} | |
}); | |
}; | |
$.fn.pluginExample.defaults = { | |
// TODO: Default values go here | |
defaultValue: 1 | |
}; | |
function PluginExample(element, options) { | |
this.init(element, options); | |
} | |
PluginExample.prototype = { | |
constructor: PluginExample, | |
init: function (element, options) { | |
this.options = options; | |
this.$element = $(element); | |
// TODO: Add initialization code here | |
}, | |
demo: function() { | |
this.$element.addClass('demo'); | |
console.log('demo'); | |
} | |
// TODO: Add additional methods here | |
}; | |
}) (jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment