Files
piratepoet/assets/js/src/ajax.js
Pavel Dohnal 699017532e Fix func-names rule in es5
Please remove those comments if you work on those files
[MAILPOET-1143]
2018-01-08 14:56:00 +00:00

88 lines
2.0 KiB
JavaScript

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