Created
May 8, 2014 14:35
-
-
Save Haraldson/59b613ec2e70ca724b42 to your computer and use it in GitHub Desktop.
Fetch
This file contains hidden or 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
function fetch(options) | |
{ | |
var ongoingAjaxFetch = this.get('ongoingAjaxFetch'); | |
if(ongoingAjaxFetch) return ongoingAjaxFetch; | |
var defaults = { includeDocs: true }; | |
options = options ? $.extend({}, defaults, options) : defaults; | |
var deferred; | |
var data = this.cache(); | |
var forceRefetch = ('refetch' in options) && options.refetch; | |
if(data && !forceRefetch) | |
{ | |
deferred = $.Deferred(); | |
deferred.resolve(data); | |
} | |
else | |
{ | |
options = $.extend({}, this.options, options); | |
deferred = App.connection.ajax.request(App.connection.url.getIssue(options), 'get'); | |
this.set('ongoingAjaxFetch', deferred); | |
} | |
$.when(deferred) | |
.done(_.bind(this.fetchSuccess, this)) | |
.fail(_.bind(this.fetchFailure, this)) | |
.always(_.bind(this.fetchAlways, this)); | |
return deferred.promise(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment