Saturday 15 February 2014

jQuery plugin and storing state of page elements -


I have a plugin that treats a text box as numerical Excel cell with a thousand separator.

The following line text box makes the price different from thousand ...

  $ ('# xlLike'). Excelify ();   

... and 'Accelived' marks the internal input tag to save state (such as: this input is accessed ).

In the following line, all such 'accelerated' inputs are found and separators are removed from their values ​​...

  $ ('[data-excelify]'). Excelify ({toNumber: true});   

This internal marking is done using .attr () which is suitable with DOM. However, I could use the .data () and mark in memory in jQuery. Finally, I could use some class names inside the plugin to mark those text boxes.

Perhaps this is not a big deal, but I want to know how to store the states right one while writing a plugin

If the PS is necessary, then the example of a plugin is given here:

- Revoked answer -

My opinion:

One of the benefits using jquery data is that you your data Can be stored in a very structured manner Although data () can be compromised outside, you can plug in as a class like on the way You can use where your values ​​are stored Not say in the object.

On the other hand, interesting prospects open using the [data-] properties when you want to manipulate the data using DOM, eg. Want to insert the Ready attribute value element or change other script values.

He said, using [data -] is not necessary in the form of storage that requires an interface like yours, where you need them To change the states for elements, the attribute value is used as a selector This is a significant difference in how to implement a plugin as you parse the DOM, which element is a specific State is load on display May effect - eg. When envisioning a spreadsheet like a web application with a large number of affected cells.

The following is a example for a plugin interface that can perform those functions better in my eyes, apart from this, it shows the use of data as an internal repository:

  (function ($) {var defaults = {// ...}; var methods = {init: function} {Var attribute = $ .extend (true, {}, Default, option); return.each (function () {var $ element = $ (this); var data = $ element.data ('foo'); // ... if (! Data) {$ (this ) .data ('foo', {'properties': property // ...}); // methods._bind.apply (this, []);}}}}; '_bind': function ( ) {Var $ element = $ (This); $ element.data ('foo', $ .extend (true, {}, $ element.data ('foo'), {// ...}); // ...}, '_inbind': function () {var $ element = $ (this); $ element.data ('foo', $ .extend (true, {}, $ Element.data ('foo'), {// .. .}}); // ...}, 'add': function () {return.Each (function () {// ... methods._bind.apply (this, []);})}}, 'Remove': function () {return.Each (function () {// ... methods._unbind.apply (this, []);});}}; $ .fn.foo = function (arg) {if (arg) {if (argof === 'string') {if (arg.indexOf ('_') == = 0} {arg = '';} (M ethods [arg]) {return methods [ag] Aply (this, array.prototype.lice.call (argument, 1)); }}} Return methods.init.apply (this, logic); }; }) (JQuery);   

Then you can use it like this:

  $ xl = $ ('.xl'); $ Xl.excelify ('Add'); $ Xl.excelify ('delete');   

or even a toggle (not applicable in the above code example):

  $ xlLike.excelify ();    

No comments:

Post a Comment