Skip to content

Instantly share code, notes, and snippets.

@odoe
Created November 2, 2015 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odoe/0672b6554346bf95b154 to your computer and use it in GitHub Desktop.
Save odoe/0672b6554346bf95b154 to your computer and use it in GitHub Desktop.
var mixin = lang.mixin;
var API_URL = 'http://jsonplaceholder.typicode.com';
var UserStore = declare(Store, {
constructor: function() {
this.apiUrl = API_URL;
this.headers = {}; // any custom headers here
},
_request: function(target, options) {
var def = new Deferred();
options = mixin({
handleAs: 'json',
url: this.apiUrl + target,
method: 'get'
}, options);
options.headers = mixin({}, options.headers, this.headers);
esriRequest(options)
.then(function(results) {
// normally do any cleanup here
// if needed
def.resolve(results);
})
.otherwise(function(err) { def.reject(err); });
return def.promise;
},
get: function(id) {
if (id == null) { // loose check, 0 could be allowed
return this.fetch();
}
return this._request('/users/' + id);
},
fetch: function() {
var queryLog = this.queryLog;
return new QueryResults(
this._request('/users')
.then(function(data) {
queryLog.map(function(entry) { data = entry.querier(data); });
return data;
}));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment