cache ajax result by localStorage
NPM:
npm install xsm-ajax-cache
With Broswerify:
var AjaxCache = require('xsm-ajax-cache');
var MyAjaxCache = AjaxCache({
key: 'my-cache',
initialize: function () {
// some thing to do before getData()
// only do it once and called just before getData()
},
ajaxParam: {
url: '/my-data-path',
dataType: 'JSON'
}
});
MyAjaxCache
.getData()
.done(function (data) {
// if current browser support localStorage &&
// last request success &&
// cache data not expired
// data is from localStorage
// else
// data is from ajax
// use data ...
})
.fail(function () {
// fail ...
});