store JSON data for later calls in jquery plugin
I'm writing a jQuery plugin that works on a piece of JSON data object. This data needs to be calculated by the plugin only once, so I want to calculate it on the first call to the plugin and store it to be used in every subsequent call. My questtion is whether there's a standard and accepted methodology for storing data used by jQuery plugins. Assuming my plugin is:
jQuery.fn.myPlugin = function(){...}
I was thinking of storing it's calculated data in:
开发者_StackOverflowjQuery.myPlugin.data = {...}
Is this the acceptable way of going about it?
I think storing it there is acceptable (or jQuery.fn.myPlugin.data
for that matter)...or instead use your own ID in $.cache
which is for storage but uses integer IDs for jQuery events and data, so you won't have any conflict, for example:
$.cache["myPlugin"] = myData;
//and to get:
var dataToUse = $.cache["myPlugin"];
The main reason I'd take this route is it eliminates the potential jQuery.something
naming conflicts that could arise with future versions.
精彩评论