Skip to content

Instantly share code, notes, and snippets.

@toranb
Created April 3, 2016 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toranb/0b4023a181ccc5869ac740894c9d2d63 to your computer and use it in GitHub Desktop.
Save toranb/0b4023a181ccc5869ac740894c9d2d63 to your computer and use it in GitHub Desktop.
a simple RSVP aware ajax helper for vanilla ajax requests
import Ember from 'ember';
var configureAjaxDefaults = function(hash) {
hash.method = hash.method || "GET";
hash.dataType = hash.dataType || "json";
hash.cache = hash.cache || false;
if(!hash.contentType && hash.data) {
hash.contentType = "application/json";
}
return hash;
};
export default function(url, method, hash) {
var self = this;
hash = hash || {};
hash.url = url;
hash.method = method;
hash = configureAjaxDefaults(hash);
return new Ember.RSVP.Promise(function(resolve, reject) {
hash.success = function(json) {
return Ember.run.join(null, resolve, json); //try w/ integration
};
hash.error = function(json, textStatus, errorThrown) {
if (json && json.then) {
json.then = null;
}
Ember.run(self, "onError", json, textStatus, errorThrown);
return Ember.run.join(null, reject, json);
};
Ember.$.ajax(hash);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment