Friday, March 1, 2013

A deferred loading cache in Javascript

I was playing around with jQuery deferreds the other day, and thought up a neat use for them, in a client-side ajax response cache. I created a simple expiring cache abstraction, and then wrote a 'loading' cache around it, which populates the cache with $.Deferred instances which are resolved with the result of a $.get.

Here's the simple expiring cache:




And here's the deferred loading cache:




Jasmine spec for the loading cache:




In my next post, I'll show how to use a jasmine spec runner for steal to run jasmine steal specs in grunt.

No comments: