Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.