Created
February 8, 2017 01:35
-
-
Save edfialk/d8a9f6ad1b8d37a25b384df7a3a06862 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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