我已经重构了工作代码以使用正确的对象,但现在我无法让 Prototype 的 AJAX.Request 正常工作。代码如下,它在 YUI 的 DataTable 上下文中工作:
SearchTable.prototype.setTableColumns = function (transport) {
this.dataTableColumns = transport.responseText.evalJSON();
this.dataTableCallback();
};
SearchTable.prototype.setTableConfiguration = function (transport) {
this.dataTableConfiguration = transport.responseText.evalJSON();
this.dataTableCallback();
};
SearchTable.prototype.show = function () {
....
new Ajax.Request(this.dataProxy, {
method: 'get',
parameters: {
format: 'json',
param: 'columns'
},
onSuccess: this.setTableColumns
});
new Ajax.Request(this.dataProxy, {
method: 'get',
parameters: {
format: 'json',
param: 'configuration'
},
onSuccess: this.setTableConfiguration
});
}
};
SearchTable.prototype.dataTableCallback = function () {
....
}
我的问题是 dataTableCallback
从未被调用。显然它抛出了一个 this
未定义的异常,我可以理解:回调不会在对象上下文中调用,因此 this
永远不会分配。我尝试过 curry 化回调但失败了。
问题是:我怎样才能做到这一点?
最佳答案
试试这个:
onSuccess: this.setTableColumns.bind(this)
关于javascript - 在 Prototype 中使用自己的 AJAX 请求回调方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1001382/