Revert "Improve ajax errors [MAILPOET-929]"

This commit is contained in:
mrcasual
2017-06-22 20:04:10 -04:00
committed by GitHub
parent 3fafe2bd6a
commit 81c3e2facf
2 changed files with 74 additions and 88 deletions

View File

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

View File

@@ -55,9 +55,6 @@ 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 %>