Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've been googling and landed here so, I have to post some ideas: first I agree with @Raynos. </p> <p>The most code out there that tries to build a jQuery plugin actually...is not a plugin! It's just an object stored in memory which is refered by the data property of a node/element. That's because jQuery should be seen and used as a tool side by side with a class library (to remedy js inconsistencies from OO architecture) to build better code and yes this is not bad at all!</p> <p>If you don't like classical OO behaviour stick to a prototypal library like <a href="http://clonejs.org/" rel="nofollow noreferrer">clone</a>.</p> <p>So what our options really?</p> <ul> <li>use JQueryUI/Widget or a similar library that hides technicalities and provides abstraction</li> <li>don't use them because of complexities, learning curve and god knows future changes</li> <li>don't use them becuase you want to insist on modular design, build small-increase later</li> <li>don't use them because you might want porting/connecting your code with different libraries.</li> </ul> <p>Suppose the issues addressed by the following scenario (see the complexities from this question: <a href="https://stackoverflow.com/questions/7127600/which-jquery-plugin-design-pattern-should-i-use">Which jQuery plugin design pattern should I use?</a>):</p> <blockquote> <p>we have nodes A, B and C that store an object reference into their <code>data</code> property</p> <blockquote> <p>some of them store info in public <strong>and</strong> private accessible <strong><em>internal objects</em></strong>, some classes of these objects are connected with <strong>inheritance</strong>, all of these nodes also need some private <strong>and</strong> public <strong><em>singletons</em></strong> to work best. </p> </blockquote> </blockquote> <p>What would we do? See the drawning:</p> <pre><code>classes : | A B C ------------------case 1---------- members | | | | of | v v v an object | var a=new A, b=new B, c=new C at | B extends A node X : | a, b, c : private ------------------case 2--------- members | | | | of | v v v an object | var aa=new A, bb=new B, cc=new C at | BB extends AA node Y : | aa, bb, cc : public -------------------case 3-------- members | | | | of | v v v an object | var d= D.getInstance() (private), at | e= E.getInstance() (public) node Z : | D, E : Singletons </code></pre> <p>as you can see every node refers to an object - a jQuery approach - but these objects change wildely; they contain object-properties with different data stored in or, even singletons that should be...single in memory like the prototype functions of the objects. We don't want every object's function belonging to <code>class A</code> to be repeatedly <strong><em>duplicated in memory</em></strong> in every node's object!</p> <p><strong><em>Before my answer</em></strong> see a common approach I've seen in jQuery plugins - some of them very popular but I don't say names:</p> <pre><code>(function($, window, document, undefined){ var x = '...', y = '...', z = '...', container, $container, options; var myPlugin = (function(){ //&lt;----the game is lost! var defaults = { }; function init(elem, options) { container = elem; $container = $(elem); options = $.extend({}, defaults, options); } return { pluginName: 'superPlugin', init: function(elem, options) { init(elem, options); } }; })(); //extend jquery $.fn.superPlugin = function(options) { return this.each(function() { var obj = Object.create(myPlugin); //&lt;---lose, lose, lose! obj.init(this, options); $(this).data(obj.pluginName, obj); }); }; }(jQuery, window, document)); </code></pre> <p>I was watching some slides at: <a href="http://www.slideshare.net/benalman/jquery-plugin-creation" rel="nofollow noreferrer">http://www.slideshare.net/benalman/jquery-plugin-creation</a> from Ben Alman where he refers at slide 13 to <strong><em>object literals</em></strong> as <strong><em>singletons</em></strong> and that just knock me over: this is what the above plugin does, it creates one singleton with <strong><em>no chance whatsover</em></strong> to alter it's internal state!!!</p> <p>Furthermore, at the jQuery part it stores a <strong><em>common reference</em></strong> to every single node!</p> <p>My solution uses a <strong><em>factory</em></strong> to keep internal state and return an object plus it can be expanded with a <strong><em>class</em></strong> library and split in different files:</p> <pre><code>;(function($, window, document, undefined){ var myPluginFactory = function(elem, options){ ........ var modelState = { options: null //collects data from user + default }; ........ function modeler(elem){ modelState.options.a = new $$.A(elem.href); modelState.options.b = $$.B.getInstance(); }; ........ return { pluginName: 'myPlugin', init: function(elem, options) { init(elem, options); }, get_a: function(){return modelState.options.a.href;}, get_b: function(){return modelState.options.b.toString();} }; }; //extend jquery $.fn.myPlugin = function(options) { return this.each(function() { var plugin = myPluginFactory(this, options); $(this).data(plugin.pluginName, plugin); }); }; }(jQuery, window, document)); </code></pre> <p>My project: <a href="https://github.com/centurianii/jsplugin" rel="nofollow noreferrer">https://github.com/centurianii/jsplugin</a></p> <p>See: <a href="http://jsfiddle.net/centurianii/s4J2H/1/" rel="nofollow noreferrer">http://jsfiddle.net/centurianii/s4J2H/1/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload