For exercise purposes, I create a jQuery plugin, a simple image slider. I use the template from the Boilerplate - jQuery plugins .
During the initialization process, everything works as expected, each instance receives the correct values โโ(width and height, as well as event bindings) needed for configuration.
The problem starts when I try to pass the calculated width of the slides to the function that performs the animation (the next button is pressed). Each value that I tried to save was overwritten with the last instance - well, that is what prototype inheritance does as I found out.
I googled a lot and found (not only) this solution in shareoverflow: global or local variables in jquery-plugin . The accepted answer suggests storing values โโthat should be private in the HTML markup, the data-whatever attribute.
This works very well, but not very nice.
The second answer seemed more elegant, preserving the real private variables within each instance:
(function($) { $.pluginName = function(element, options) { .... var plugin = this; ....
I looked quite simply - but I can not implement this solution!
So my question is: how do I implement a private variable in the following plugin template? (And how do I properly name it from inside Plugin.prototype ...)
I created Fiddle with my plugin. Code Summary:
(function ($, window, document, undefined) { var pluginName = "easyCarousel"; // default configuration object defaults = { ... default values here, css etc } }; // The actual plugin constructor function Plugin(element, options) { this.element = element; this.options = $.extend({}, defaults, options); this._defaults = defaults; this._name = pluginName; this.init(); } Plugin.prototype = { init: function () { var options = this.options; this.options.stage = $(this.element); .... calling/starting views and controllers }, buildCollectionView: function (options, Helper) { .... setting markup, applying css, calulating dimensions }, buildStageView: function (options, Helper) { .... setting markup, applying css .... storing calculated slide width in data Attribute stage.attr({ 'data-slidewidth': width, 'data-stagewidth': setWidth }); }, buildTheatreView: function (options, Helper) { .... setting markup, applying css }, buildNavigationView: function (options) { .... setting markup }, navigationController: function (options, slideForward, slideBackward) { .... event binding pubSub.on("navigation:arrownext:click", function (e) { slideForward(options, e); }); $('.carousel__arrownext', options.theatre) .bind('click', function () { pubSub.trigger("navigation:arrownext:click", this); }); }, slideForwardAnimation: function (options, e) { .... reading stored width value from data Attribute $element.animate({ "left": "-=" + slideWidth }) }, setDimensionsHelper: function (options) { .... calculating an returning dimensions of the slider }, eventBusHelper: function (options) { // integrating third party eventbus } }; $.fn[pluginName] = function (options) { return this.each(function () { if (!$.data(this, "plugin_" + pluginName)) { $.data(this, "plugin_" + pluginName, new Plugin(this, options)); } }); }})(jQuery, window, document);
I hope my sample code will not be complicated / long to view.
As you can see, I'm new to advanced Javascript templates - and now I'm really stuck. I spend many hours on this problem. Therefore, every attempt to help is greatly appreciated. Also, of course, every code review.
Thank you in advance:)