我正在编写我的代码如下:
var MyLib = (function (window, $, undefined) {
return {
URI: 'http://testpage/API/',
OnSuccess: function (data, status) { },
OnError: function (request, status, error) { },
MakeRequest: function (method, args) {
$.ajax({
type: 'POST',
url: this.URI + '/' + method,
contentType: 'application/json; charset=utf-8',
data: args,
dataType: 'json',
success: this.OnSuccess,
error: this.OnError
});
},
GetSamples: function (data1, data2) {
var args = {
data1: data1,
data2: data2
};
this.MakeRequest('GetTestData', JSON.stringify(args));
}
};
} (this, jQuery));
因此,如果我想调用 AJAX 调用,我会这样做:
function OnSuccess(data, status) {
// ...
}
function OnError(request, status, error) {
}
MyLib.OnSuccess = OnSuccess;
MyLib.OnError = OnError;
MyLib.GetSamples("data1", "data2");
我不想更改 GetSamples 的签名,因此我选择按上述方式实现它。关于这是否是一种可接受的方法(或如何改进)的任何建议?