Created
November 2, 2015 18:34
-
-
Save odoe/0672b6554346bf95b154 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
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