Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Edit:</p> <p>After searching for threads on <a href="https://stackoverflow.com/questions/11449538/jquery-plugin-template">jQuery plugin template</a> I found these <a href="http://jqueryboilerplate.com/" rel="nofollow noreferrer">Boilerplate templates</a> which are more versatile and extensive designs than what I've offered below. Ultimately what you choose all depends on what your needs are. The Boilerplate templates cover more use cases than my offering, but each has its own benefits and caveats depending on the requirements.</p> <hr> <p>Typically jQuery plugins either return a jQuery object when a value is passed to them as in:</p> <pre><code>.wrap(html) // returns a jQuery object </code></pre> <p>or they return a value when no parameter is passed in</p> <pre><code>.width() // returns a value .height() // also returns a value </code></pre> <hr> <p>To read your example calling convention:</p> <blockquote> <p><code>$('#test1').myplugin().changeBorder($('#inpt').val());</code></p> </blockquote> <p>it would appear, to any developer who uses jQuery, as though two separate plugins are being utilized in tandem, first <code>.myplugin()</code> which one would assume will return a jQuery object with some default DOM maniplulation performed on <code>#test1</code>, then followed by <code>.changeBorder($('#inpt').val())</code> which may also return a jQuery object but in the case of your example the whole line is not assigned to a variable so any return value is not used - again it looks like a DOM manipulation. But your design does not follow the standard calling convention that I've described, so there may be some confusion to anyone looking at your code as to what it actually does if they are not familiar with your plugin.</p> <hr> <p>I have, in the past, considered a similar problem and use case to the one you are describing and I like the idea of having a convenient convention for calling separate functions associated with a plugin. The choice is totally up to you - it is your plugin and you will need to decide based on who will be using it, but the way that I have settled on is to simply pass the name of the function and it's parameters either as a separate <code>.myplugin(name, parameters)</code> or in an object as <code>.myplugin(object)</code>.</p> <p>I typically do it like so:</p> <pre><code>(function($) { $.fn.myplugin = function(fn, o) { // both fn and o are [optional] return this.each(function(){ // each() allows you to keep internal data separate for each DOM object that's being manipulated in case the jQuery object (from the original selector that generated this jQuery) is being referenced for later use var $this = $(this); // in case $this is referenced in the short cuts // short cut methods if(fn==="method1") { if ($this.data("method1")) // if not initialized method invocation fails $this.data("method1")() // the () invokes the method passing user options } else if(fn==="method2") { if ($this.data("method2")) $this.data("method2")() } else if(fn==="method3") { if ($this.data("method3")) $this.data("method3")(o) // passing the user options to the method } else if(fn==="destroy") { if ($this.data("destroy")) $this.data("destroy")() } // continue with initial configuration var _data1, _data2, _default = { // contains all default parameters for any functions that may be called param1: "value #1", param2: "value #2", }, _options = { param1: (o===undefined) ? _default.param1 : (o.param1===undefined) ? _default.param1 : o.param1, param2: (o===undefined) ? _default.param2 : (o.param2===undefined) ? _default.param2 : o.param2, } method1 = function(){ // do something that requires no parameters return; }, method2 = function(){ // do some other thing that requires no parameters return; }, method3 = function(){ // does something with param1 // _options can be reset from the user options parameter - (o) - from within any of these methods as is done above return; }, initialize = function(){ // may or may not use data1, data2, param1 and param2 $this .data("method1", method1) .data("method2", method2) .data("method3", method3) .data("destroy", destroy); }, destroy = function(){ // be sure to unbind any events that were bound in initialize(), then: $this .removeData("method1", method1) .removeData("method2", method2) .removeData("method3", method3) .removeData("destroy", destroy); } initialize(); }) // end of each() } // end of function })(jQuery); </code></pre> <hr> <p>And the usage:</p> <pre><code>var $test = $('#test').myplugin(false, {param1: 'first value', param2: 'second value'}); // initializes the object $test.myplugin('method3', {param1: 'some new value', param2: 'second new value'}); // change some values (method invocation with params) </code></pre> <p>or you could just say:</p> <pre><code>$('#test').myplugin(); // assume defaults and initialize the selector </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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