Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, you will generally want to handle the command within your extension method. Second, you should be attaching configurations to each item...</p> <pre><code>(function($){ var defaultOptions = { /* default settings here */ }; //called on the native object directly, wrap with $(obj) if needed. function initializeObject(obj, options) { //assign the options to the object instance. $(obj).data('myPlugin-options', $.extend(defaultOptions, options) ); //do other initialization tasks on the individual item here... } function updateObject(obj) { // use $(obj).data('myPlugin-options'); } function setOption(obj, key, value) { var d = $(obj).data('myPlugin-options'); d[key] = value; $(obj).data('myPlugin-options', d); } $.fn.myPlugin = function(command, option, val) { if (typeof command == "object") { //initialization return this.each(function(){ initializeObject(this, command); }); } if (typeof command == "string") { // method or argument query switch (command.toLowerCase()) { case 'option': //get value, return the first item's value if (typeof val == undefined) return this.eq(0).data('myPlugin-options')[option]; //set option value return this.each(function() { setOption(this, option, val); }); case 'update': return this.each(function() { updateObject(this); }); //other commands here. } } } })(jQuery) </code></pre> <p>With the above example, you have a generic template for a jQuery extension, It's usually good form to have the following convention for use..</p> <pre><code>Initialization: $(...).myPlugin({ initialization-options-here }); Command: $(...).myPlugin('command-name'); //where command can be update, etc. Get Option: var myValue = $(...).myPlugin('option', 'option-name'); Set Option: $(...).myPlugin('option', 'option-name', newValue); </code></pre> <p>Updated to use .data off of each individual obj.</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. 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.
 

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