The Boilerplate ?
The Boilerplate ?
starting with an empty boilerplate like this one above, I want to call this plugin on one specific div :
- <div id="BoilerplateEntryPoint">BoilerplateEntryPoint</div>
thus that is may code, first the text BoilerplateEntryPoint is set to pink color (OK), then I check that no data exist with "plugin_defaultPluginName" (OK -> alert box tells me "undefined"), then I call the plugin defaultPluginName on that specific div $('#BoilerplateEntryPoint'), and to check that the code is well execute, I start the plugin constructor with $('#BoilerplateEntryPoint').css("color", "red"); (BUT THE COLOR TEXT IS STILL PINK, NOT RED, as if the plugin constructor was not called).
- $('#BoilerplateEntryPoint').css("color", "pink");
- alert($.data(this, "plugin_defaultPluginName"));
- $('#BoilerplateEntryPoint').defaultPluginName({
- propertyName: 'a custom value'
- });
- alert($.data(this, "plugin_defaultPluginName"));
- ;(function ($, window, document, undefined) {
- var pluginName = "defaultPluginName";
- var defaults = {
- propertyName: "value"
- };
- function Plugin(element, options) {
- this.element = element;
- this.options = $.extend({}, defaults, options);
- this._defaults = defaults;
- this._name = pluginName;
- this.init();
- }
- Plugin.prototype = {
- init: function () {
- $(this).css("color", "red");
- // code goes here
- },
- yourOtherFunction: function () {
- // some logic
- }
- };
- $.fn[pluginName] = function (options) {
- $('#BoilerplateEntryPoint').css("color", "red");
- return this.each(function () {
- if (!$.data(this, "plugin_" + pluginName)) {
- $.data(this, "plugin_" + pluginName, new Plugin(this, options));
- }
- });
- };
- })(jQuery, window, document);