Skip to content

Instantly share code, notes, and snippets.

@edfialk
Created February 8, 2017 01:35
Show Gist options
  • Save edfialk/d8a9f6ad1b8d37a25b384df7a3a06862 to your computer and use it in GitHub Desktop.
Save edfialk/d8a9f6ad1b8d37a25b384df7a3a06862 to your computer and use it in GitHub Desktop.
/**
* Store.js
* Simple caching for api requests using ES6 Promise
* I used jquery for ajax because of Bootstrap
* store.fetch(val).then(this.handleResponse);
*/
const apiroot = '/api/v1/';
const cache = Object.create(null);
export default {
fetch(query) {
return new Promise((resolve, reject) => {
if (cache[query]) {
resolve(cache[query]);
} else {
$.getJSON(apiroot + query, json => {
resolve(cache[query] = json);
}).fail( (jqxhr, text, error) => {
reject(text);
});
}
});
},
/* other store methods */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment