fixed MPAjax to behave with router

This commit is contained in:
Jonathan Labreuille
2015-08-18 15:29:00 +02:00
parent e925b14aae
commit 58d83f5fbe
3 changed files with 37 additions and 39 deletions

View File

@ -1,16 +1,12 @@
define('ajax', ['mailpoet', 'jquery'], function(MailPoet, jQuery) {
"use strict";
/**
* MailPoet Ajax
**/
MailPoet.Ajax = {
version: 0.1,
options: {},
defaults: {
url: null,
controller: 'dummy',
action: 'test',
endpoint: null,
action: null,
data: {},
onSuccess: function(data, textStatus, xhr) {},
onError: function(xhr, textStatus, errorThrown) {}
@ -28,39 +24,41 @@ define('ajax', ['mailpoet', 'jquery'], function(MailPoet, jQuery) {
// merge options
this.options = jQuery.extend({}, this.defaults, options);
// set default url
if(this.options.url === null) {
this.options.url = ajaxurl+'?action=mailpoet_ajax';
this.options.url = ajaxurl;
}
// routing
this.options.url += '&mailpoet_controller='+this.options.controller;
this.options.url += '&mailpoet_action='+this.options.action;
},
request: function(method, options) {
// set options
this.init(options);
// set request params
var params = {
action: 'mailpoet',
token: mailpoet_token,
endpoint: this.options.endpoint,
method: this.options.action,
data: this.options.data
};
// make ajax request depending on method
if(method === 'get') {
jQuery.get(
this.options.url,
this.options.data,
params,
this.options.onSuccess,
'json'
);
} else {
jQuery.ajax(
this.options.url,
{
data: JSON.stringify(this.options.data),
processData: false,
contentType: "application/json; charset=utf-8",
type : method,
dataType: 'json',
success : this.options.onSuccess,
error : this.options.onError
}
);
jQuery.ajax({
url: this.options.url,
type : 'post',
data: params,
dataType: 'json',
success : this.options.onSuccess,
error : this.options.onError
});
}
}
};

View File

@ -23,9 +23,9 @@ class Router {
$class = ucfirst($_POST['endpoint']);
$endpoint = __NAMESPACE__ . "\\" . $class;
$method = $_POST['method'];
$args = $_POST['args'];
$data = $_POST['data'];
$endpoint = new $endpoint();
$endpoint->$method($args);
$endpoint->$method($data);
}
function setToken() {

View File

@ -33,22 +33,22 @@
</form>
<script type="text/javascript">
jQuery(document).ready(function($) {
$.ajax({
url: ajaxurl,
type: 'post',
data: {
action: 'mailpoet',
token: mailpoet_token,
jQuery(function($) {
// dom loaded
$(function() {
MailPoet.Ajax.post({
endpoint: 'settings',
method: 'get',
args: {
action: 'get',
data: {
first_name: 'John'
},
onSuccess: function(response) {
// console.log(response);
}
},
success : function(response) {
console.log(response);
}
});
});
});
</script>