Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>-- REWRITTEN ANSWER --</p> <p>My opinion:</p> <p>One of the advantages when using jquery <code>data()</code> is that you can <strong>store your data in a very structured manner</strong>. Although <code>data()</code> can be manipulated from outside you can use the plugin in a <em>class-like</em> way, where you have your values stored <em>lets say</em> inside the object.</p> <p>On the other hand using <code>[data-]</code> attributes opens up interesting possibilities when you want to manipulate the data from outside using the DOM, e.g. when inserting elements with a <em>prepared</em> attribute value or want other scripts to change values etc..</p> <p>That said, <strong>using the <code>[data-]</code> attribute as storage does not necessarily require having an interface like yours where you have to use the attribute value as selector to change states for those elements</strong>. This is a crucial difference in how you apply a plugin. As you parse the DOM again to find out which elements have a specific state, it may have a huge impact on performance - e.g. when imagining a spreadsheet like web application with large amounts of affected cells.</p> <p>The following is an <strong>example</strong> for a plugin interface which can perform those tasks much better in my eyes. Further more it demonstrates the usage of data as internal storage:</p> <pre><code>(function($){ var defaults = { //... }; var methods = { init : function(options){ var properties = $.extend(true, {}, defaults, options); return this.each(function(){ var $element = $(this); var data = $element.data('foo'); //... if (!data) { $(this).data('foo', { 'properties' : properties //... }); //... methods._bind.apply(this, []); } }); }, '_bind' : function(){ var $element = $(this); $element.data('foo', $.extend(true, {}, $element.data('foo'), { //... })); //... }, '_unbind' : function(){ var $element = $(this); $element.data('foo', $.extend(true, {}, $element.data('foo'), { //... })); //... }, 'add' : function(){ return this.each(function(){ //... methods._bind.apply(this, []); }); }, 'remove' : function(){ return this.each(function(){ //... methods._unbind.apply(this, []); }); } }; $.fn.foo = function(arg){ if (arg) { if (typeof arg === 'string') { if (arg.indexOf('_') === 0) { arg = ''; } if (methods[arg]) { return methods[arg].apply(this, Array.prototype.slice.call(arguments, 1)); } } } return methods.init.apply(this, arguments); }; })(jQuery); </code></pre> <p>So you can then use it like this:</p> <pre><code>$xl = $('.xl'); $xl.excelify('add'); $xl.excelify('remove'); </code></pre> <p>Or even a toggle (not implemented in the above code example):</p> <pre><code>$xlLike.excelify(); </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.
 

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