11 January 2009

jQuery Object Cache Update

After discussing with Andrew Luetgers some improvements that could be done to the Object Cache plugin, I updated the plugin to a new version.

The new improvement is that now you can store a selection automatically:

// Will store in cache $("#sidebarNav") with #sidebarNav key
$$("#sidebarNav");

This will let you avoid doing $("#menu").cache("menu"), which is a bit redundant. Now by doing $$("#menu") you will be able to store it in the cache and retrieve it automatically. There is also a reload option that you can pass to that call in order to, well, reload the the cached object with that selection:

// Will reload #sidebarNav with $("#sidebarNav")
$$("#sidebarNav", true);

You can grab the new version of the Object Cache plugin here.


20 November 2008

New jQuery plugin: Object Cache

Inspired by Benjamin Sterling's "Better jQuery Code" article I decided to develop a simple plugin to make his first point (Caching) easier... nothing fancy, just a few methods, but you will hopefully find it useful.

Its objective is to let you store a jQuery object with a simple key in a global cache, so that you can access the same object easily, without having to write the same selection, filtering or traversing code (i.e: $("#main < p") or $("#main").children(".selected").eq(0)).

Here is how it works:

// Store in cache - Returns current object
$("#mainNav").cache("main_navigation");

// Retrieve from cache - Returns cached object
$$("main_navigation"); // or jQueryCache("main_navigation");

// Remove from cache
$$.remove("main_navigation");

// Clear Cache
$$.clear();

// Load jQueryCache with noConflict to avoid overriding window.$$
$$.noConflict();

There is a lot of room for improvement, which will be done depending on the feedback I get, so feel free to contact me with any ideas or corrections you might come up with.

You can get the Jquery Object Cache plugin here.