Expose jQuery deferred XHR object for access to promises

This commit is contained in:
Tautvidas Sipavičius
2015-09-02 17:53:57 +03:00
parent e0ef01d9f8
commit e20626f1ad

View File

@ -13,13 +13,13 @@ define('ajax', ['mailpoet', 'jquery'], function(MailPoet, jQuery) {
onError: function(xhr, textStatus, errorThrown) {} onError: function(xhr, textStatus, errorThrown) {}
}, },
get: function(options) { get: function(options) {
this.request('get', options); return this.request('get', options);
}, },
post: function(options) { post: function(options) {
this.request('post', options); return this.request('post', options);
}, },
delete: function(options) { delete: function(options) {
this.request('delete', options); return this.request('delete', options);
}, },
init: function(options) { init: function(options) {
// merge options // merge options
@ -46,18 +46,18 @@ define('ajax', ['mailpoet', 'jquery'], function(MailPoet, jQuery) {
endpoint: this.options.endpoint, endpoint: this.options.endpoint,
method: this.options.action, method: this.options.action,
data: this.options.data || {} data: this.options.data || {}
}; }, jqXHR;
// make ajax request depending on method // make ajax request depending on method
if(method === 'get') { if(method === 'get') {
jQuery.get( jqXHR = jQuery.get(
this.options.url, this.options.url,
params, params,
this.options.onSuccess, this.options.onSuccess,
'json' 'json'
); );
} else { } else {
jQuery.ajax({ jqXHR = jQuery.ajax({
url: this.options.url, url: this.options.url,
type : 'post', type : 'post',
data: params, data: params,
@ -69,6 +69,8 @@ define('ajax', ['mailpoet', 'jquery'], function(MailPoet, jQuery) {
// clear options // clear options
this.options = {}; this.options = {};
return jqXHR;
} }
}; };
}); });