Created
May 26, 2014 10:08
-
-
Save Haraldson/908c5d1f0cafd5cd765c to your computer and use it in GitHub Desktop.
Fetch using promises
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
| 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