您需要覆盖Backbone.sync
函数更改用于AJAX调用的URL。您可以从插件中学习wp-backbone 这样做,甚至更多。下面,您可以从中看到它如何将所有操作更改为POST
或GET
, 创建参数,添加action
参数等。
(function($) {
Backbone.sync = function(method, model, options) {
var params = _.extend({
type: \'POST\',
dataType: \'json\',
url: wpBackboneGlobals.ajaxurl,
contentType: \'application/x-www-form-urlencoded;charset=UTF-8\'
}, options);
if (method == \'read\') {
params.type = \'GET\';
params.data = model.id
}
if (!params.data && model && (method == \'create\' || method == \'update\' || method == \'delete\')) {
params.data = JSON.stringify(model.toJSON());
}
if (params.type !== \'GET\') {
params.processData = false;
}
params.data = $.param({action:\'backbone\',backbone_method:method,backbone_model:model.dbModel,content:params.data});
// Make the request.
return $.ajax(params);
};
})(jQuery);