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) { define('ajax', ['mailpoet', 'jquery'], function(MailPoet, jQuery) {
"use strict"; "use strict";
/**
* MailPoet Ajax
**/
MailPoet.Ajax = { MailPoet.Ajax = {
version: 0.1, version: 0.1,
options: {}, options: {},
defaults: { defaults: {
url: null, url: null,
controller: 'dummy', endpoint: null,
action: 'test', action: null,
data: {}, data: {},
onSuccess: function(data, textStatus, xhr) {}, onSuccess: function(data, textStatus, xhr) {},
onError: function(xhr, textStatus, errorThrown) {} onError: function(xhr, textStatus, errorThrown) {}
@ -28,39 +24,41 @@ define('ajax', ['mailpoet', 'jquery'], function(MailPoet, jQuery) {
// merge options // merge options
this.options = jQuery.extend({}, this.defaults, options); this.options = jQuery.extend({}, this.defaults, options);
// set default url
if(this.options.url === null) { 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) { request: function(method, options) {
// set options // set options
this.init(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 // make ajax request depending on method
if(method === 'get') { if(method === 'get') {
jQuery.get( jQuery.get(
this.options.url, this.options.url,
this.options.data, params,
this.options.onSuccess, this.options.onSuccess,
'json' 'json'
); );
} else { } else {
jQuery.ajax( jQuery.ajax({
this.options.url, url: this.options.url,
{ type : 'post',
data: JSON.stringify(this.options.data), data: params,
processData: false, dataType: 'json',
contentType: "application/json; charset=utf-8", success : this.options.onSuccess,
type : method, error : this.options.onError
dataType: 'json', });
success : this.options.onSuccess,
error : this.options.onError
}
);
} }
} }
}; };

View File

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

View File

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