Using the jQuery data method as a local datastore
October 15th, 2009At some point, we all have to store datasets on the client. We can clutter the namespace with ever more variables and try really hard to avoid collisions. We can cram variables into some local object that we’re using in the normal execution of our script anyway. We can attach custom attributes to HTML nodes, violating the sacrosanct purity of XHTML.
Or we can attach the document-specific variables, in script (thus keeping those automated validators happy), to the document itself. This fairly intuitive approach has not been given the notice it deserves.
Some advantages:
- Arbitrary value names can be assigned and we can basically assign as many of these variables as we want.
- They can be applied to anything jQuery can access, meaning that we can rapidly assign values to entire sets of DOM nodes without having to add CSS classes or clutter the markup.
- They can be dynamically added, accessed, changed and removed at will, without network transfers.
There is the disadvantage that they are not persistent across page loads, but we’ve lived with that problem for a long, long time, haven’t we?
So how do we use this handy method? The documentation is pretty clear cut, but I’ll give a slightly more grounded example:
My HTML is very simplistic in this example:
1 2 3 4 5 6 7 8 | <body> <div> <span id="stats"></span> <button id="add">Add Data Point</button> <button id="reset">Reset Set</button> </div> <div id="data"></div> </body> |
The JavaScript is similarly intuitive:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | appExample = { addDataPoint: function() { var dat = this.getDataSet(); dat[dat.length] = Math.floor(Math.random()*101); $('#data').data('points', dat); }, resetDataSet: function() { $('#data').removeData('points'); }, dataSize: function() { var dat = this.getDataSet(); return dat.length; }, getDataSet: function() { var dat = $('#data').data('points'); if (undefined == dat) return []; if (Array != dat.constructor) return [dat]; return dat; }, updateDisplay: function() { $('#stats').text('Data Points: ' + (lim = this.dataSize())); var tmp = ''; var dat = this.getDataSet(); for (var i = 0; i < lim; i++) { tmp += dat[i]; if (i < lim - 1) tmp += ','; } $('#data').text('[' + tmp + ']'); } } $(document).ready( function() { appExample.updateDisplay(); $('#add').bind('click', function(e) { appExample.addDataPoint(); appExample.updateDisplay(); }); $('#reset').bind('click', function(e) { appExample.resetDataSet(); appExample.updateDisplay(); }); }); |
In this case, I’m storing everything as an array in points. While I’m storing randomly generated integers, you can just as easily store entire objects, opening the possibility of a JSON solution.
Note that we can identify those objects lacking the chosen dynamic attribute by calling it and testing for undefined.


Previous Post
Next Post

1 Comment
March 30th, 2010 at 11:16 pm
I was wondering about the if statement in the getDataSet function where it returns the dat variable in the array. What does that do? Are you creating an array if it’s not one?
Leave a Reply