Skip to content

Instantly share code, notes, and snippets.

@Haraldson
Created May 8, 2014 14:35
Show Gist options
  • Save Haraldson/59b613ec2e70ca724b42 to your computer and use it in GitHub Desktop.
Save Haraldson/59b613ec2e70ca724b42 to your computer and use it in GitHub Desktop.
Fetch
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