Revert "Revert "Improve ajax errors [MAILPOET-929]""

This reverts commit 81c3e2facf.
This commit is contained in:
Pavel Dohnal
2017-06-26 13:51:53 +01:00
parent 836b7179e9
commit 2285c08c01
2 changed files with 88 additions and 74 deletions

View File

@@ -1,74 +1,85 @@
define('ajax', ['mailpoet', 'jquery', 'underscore'], function(MailPoet, jQuery, _) { function requestFailed(errorMessage, xhr) {
'use strict'; if (xhr.responseJSON) {
MailPoet.Ajax = { return xhr.responseJSON;
version: 0.5, }
options: {}, var message = errorMessage.replace("%d", xhr.status);
defaults: { return {
url: null, errors: [
api_version: null, {
endpoint: null, message: message
action: null, }
token: null, ]
data: {} }
}, }
post: function(options) {
return this.request('post', options); define('ajax', ['mailpoet', 'jquery', 'underscore'], function(MailPoet, jQuery, _) {
},
init: function(options) { MailPoet.Ajax = {
// merge options version: 0.5,
this.options = jQuery.extend({}, this.defaults, options); options: {},
defaults: {
// set default url url: null,
if(this.options.url === null) { api_version: null,
this.options.url = ajaxurl; endpoint: null,
} action: null,
token: null,
// set default token data: {}
if(this.options.token === null) { },
this.options.token = window.mailpoet_token; post: function(options) {
} return this.request('post', options);
}, },
getParams: function() { init: function(options) {
return { // merge options
action: 'mailpoet', this.options = jQuery.extend({}, this.defaults, options);
api_version: this.options.api_version,
token: this.options.token, // set default url
endpoint: this.options.endpoint, if(this.options.url === null) {
method: this.options.action, this.options.url = ajaxurl;
data: this.options.data || {} }
}
}, // set default token
request: function(method, options) { if(this.options.token === null) {
// set options this.options.token = window.mailpoet_token;
this.init(options); }
},
// set request params getParams: function() {
var params = this.getParams(); return {
var deferred = jQuery.Deferred(); action: 'mailpoet',
api_version: this.options.api_version,
// remove null values from the data object token: this.options.token,
if (_.isObject(params.data)) { endpoint: this.options.endpoint,
params.data = _.pick(params.data, function(value) { method: this.options.action,
return (value !== null) data: this.options.data || {}
}) }
} },
request: function(method, options) {
// ajax request // set options
deferred = jQuery.post( this.init(options);
this.options.url,
params, // set request params
null, var params = this.getParams();
'json'
).then(function(data) { // remove null values from the data object
return data; if (_.isObject(params.data)) {
}, function(xhr) { params.data = _.pick(params.data, function(value) {
return xhr.responseJSON; return (value !== null)
}); })
}
// clear options
this.options = {}; // ajax request
var deferred = jQuery.post(
return deferred; this.options.url,
} params,
}; null,
}); 'json'
).then(function(data) {
return data;
}, _.partial(requestFailed, MailPoet.I18n.t('ajaxFailedErrorMessage')));
// clear options
this.options = {};
return deferred;
}
};
});

View File

@@ -55,6 +55,9 @@ jQuery('.toplevel_page_mailpoet-newsletters.menu-top-last')
'mailpoet.js' 'mailpoet.js'
)%> )%>
<%= localize({
'ajaxFailedErrorMessage': __('An error has happened while performing a request, the server has responded with response code %d'),
}) %>
<% block translations %><% endblock %> <% block translations %><% endblock %>
<% block after_translations %><% endblock %> <% block after_translations %><% endblock %>