Skip to content

Instantly share code, notes, and snippets.

@zoghal
Forked from krisselden/gist:1473687
Created June 4, 2012 20:35
Show Gist options
  • Save zoghal/2870715 to your computer and use it in GitHub Desktop.
Save zoghal/2870715 to your computer and use it in GitHub Desktop.
Ember ajax wrapper
App.ajax = (url, settings)->
deferred = $.Deferred()
settings.xhr = ->
xhr = $.ajaxSettings.xhr()
if 'onprogress' of xhr
context = this
xhr.onprogress = (e)->
Ember.run(deferred, deferred.notifyWith, context, [e.loaded/e.total])
xhr
jqXHR = $.ajax(url, settings)
jqXHR.done ->
Ember.run(deferred, deferred.resolveWith, this, arguments)
jqXHR.fail ->
Ember.run(deferred, deferred.rejectWith, this, arguments)
deferred.promise()
App.ajax = function(url, settings) {
var deferred, jqXHR;
deferred = $.Deferred();
settings.xhr = function() {
var context, xhr;
xhr = $.ajaxSettings.xhr();
if ('onprogress' in xhr) {
context = this;
xhr.onprogress = function(e) {
return Ember.run(deferred, deferred.notifyWith, context, [e.loaded / e.total]);
};
}
return xhr;
};
jqXHR = $.ajax(url, settings);
jqXHR.done(function() {
return Ember.run(deferred, deferred.resolveWith, this, arguments);
});
jqXHR.fail(function() {
return Ember.run(deferred, deferred.rejectWith, this, arguments);
});
return deferred.promise();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment