프로토 타입에서 AJAX 요청에 자체 콜백 방법을 사용합니다
-
05-07-2019 - |
문제
적절한 개체를 사용하기 위해 작업 코드를 리팩토링했으며 이제 프로토 타입의 Ajax.request를 올바르게 작동시킬 수 없습니다. 코드는 아래에 있으며 Yui의 데이터 가능과 관련하여 작동합니다.
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
할당되지 않았습니다. 카릴 콜백을 시도했지만 실패했습니다.
문제는 다음과 같습니다. 어떻게이 작업을 수행 할 수 있습니까?
해결책
"this"에 대한 폐쇄를 만듭니다.
SearchTable.prototype.show = function () {
....
var self = this;
new Ajax.Request(this.dataProxy, {
method: 'get',
parameters: {
format: 'json',
param: 'columns'
},
onSuccess: function(transport) { self.setTableColumns(transport); }
});
다른 팁
이 시도:
onSuccess: this.setTableColumns.bind(this)
제휴하지 않습니다 StackOverflow