Skip to content

Instantly share code, notes, and snippets.

@Psvensso
Last active December 19, 2015 02:29
Show Gist options
  • Save Psvensso/5883616 to your computer and use it in GitHub Desktop.
Save Psvensso/5883616 to your computer and use it in GitHub Desktop.
Ember Cache
ajax: function (url, type, hash, cache) {
var adapter = this;
function rsvp(resolve, reject, url, type, hash, cache, adapter) {
if (!!cache) {
hash.localCache = true;
hash.cacheTTL = 12; // in hours.
hash.cacheKey = 'ApplicAd-DataCache-' + url;
}
hash = hash || {};
hash.url = url;
hash.type = type;
hash.dataType = 'json';
hash.context = adapter;
if (hash.data && type !== 'GET') {
hash.contentType = 'application/json; charset=utf-8';
hash.data = JSON.stringify(hash.data);
}
hash.success = function (json) {
Ember.run(null, resolve, json);
};
hash.error = function (jqXHR, textStatus, errorThrown) {
Ember.run(null, reject, jqXHR);
};
Ember.$.ajax(hash);
}
return new Ember.RSVP.Promise(function (resolve, reject) {
rsvp(resolve, reject, url, type, hash, cache, adapter);
});
},
findAll: function (store, type, since) {
var root, adapter, pluralize = false;;
root = this.rootForType(type);
adapter = this;
return this.ajax(this.buildURL(root), "GET", {
data: this.sinceQuery(since)
}, !!type.cacheOnAll).then(function (json) {
if (!Ember.isNone(type.addRootNode) && type.addRootNode)
json = adapter.addRootNode(json, type, pluralize);
adapter.didFindAll(store, type, json);
}).then(null, DS.rejectionHandler);
},
$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
if (!Modernizr.localstorage || !options.localCache) return;
var hourstl = options.cacheTTL || 5;
var cacheKey = options.cacheKey ||
options.url.replace(/jQuery.*/, '') + options.type + options.data;
if (options.isCacheValid && !options.isCacheValid()) {
localStorage.removeItem(cacheKey);
}
var ttl = localStorage.getItem(cacheKey + 'cachettl');
if (ttl && ttl < +new Date()) {
localStorage.removeItem(cacheKey);
localStorage.removeItem(cacheKey + 'cachettl');
ttl = 'expired';
}
var value = localStorage.getItem(cacheKey);
if (value) {
if (options.dataType.indexOf('json') === 0) value = JSON.parse(value);
options.success(value);
jqXHR.abort();
} else {
if (options.success) {
options.realsuccess = options.success;
}
options.success = function (data) {
var strdata = data;
if (options.dataType.indexOf('json') === 0) strdata = JSON.stringify(data);
try {
localStorage.setItem(cacheKey, strdata);
} catch (e) {
localStorage.removeItem(cacheKey);
localStorage.removeItem(cacheKey + 'cachettl');
if (options.cacheError) options.cacheError(e, cacheKey, strdata);
}
if (options.realsuccess) options.realsuccess(data);
};
if (!ttl || ttl === 'expired') {
localStorage.setItem(cacheKey + 'cachettl', +new Date() + 1000 * 60 * 60 * hourstl);
}
}
});
YourModel.reopenClass({
cacheOnAll: true
});
loadLanguages: function () {
var that = this;
$.ajax({
url: "/api/Resource/Text_UI_ApplicAd",
localCache: true, // required to use
dataType: 'json',
cacheTTL: 1, // in hours. Optional
cacheKey: 'ApplicAdTextResources', // optional
success: function (reply) {
that._parseLanguages(reply);
}
});
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment