Skip to content

Instantly share code, notes, and snippets.

@Haraldson
Created May 26, 2014 10:08
Show Gist options
  • Select an option

  • Save Haraldson/908c5d1f0cafd5cd765c to your computer and use it in GitHub Desktop.

Select an option

Save Haraldson/908c5d1f0cafd5cd765c to your computer and use it in GitHub Desktop.
Fetch using promises
fetch: function()
{
var deferred;
var data = this.cache();
if(data)
{
deferred = $.Deferred();
deferred.resolve(data);
}
else
{
var ongoingAjaxFetch = this.get('ongoingAjaxFetch');
if(ongoingAjaxFetch) return ongoingAjaxFetch;
deferred = $.ajax({ ... });
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