我正在開發一個開源的EmberJS項目,該項目對信息進行Ajax請求,然後需要根據響應的動態子部分進行解析。Javascript:將變量傳遞給Promise
return new Promise((resolve, reject) => {
const { resourceName, identificationAttributeName } = this.getProperties('resourceName', 'identificationAttributeName');
const data = {};
data[resourceName] = { password };
data[resourceName][identificationAttributeName] = identification;
return this.makeRequest(data).then(
(response) => run(null, resolve, response),
(xhr) => run(null, reject, xhr.responseJSON || xhr.responseText)
);
});
....
makeRequest(data, options) {
const serverTokenEndpoint = this.get('serverTokenEndpoint');
const requestOptions = $.extend({}, {
url: serverTokenEndpoint,
type: 'POST',
dataType: 'json',
data,
beforeSend(xhr, settings) {
xhr.setRequestHeader('Accept', settings.accepts.json);
}
}, options || {});
return $.ajax(requestOptions);
}
最後,我需要成功應對像
(response) => run(null, resolve, response[resourceName])
運行的東西,但響應函數裏面,我到沒有接入資源名稱。我將如何發送?
這裏是transpiled代碼:
var _this = this;
return new Promise(function (resolve, reject) {
var _getProperties2 = _this.getProperties('resourceName', 'identificationAttributeName');
var resourceName = _getProperties2.resourceName;
var identificationAttributeName = _getProperties2.identificationAttributeName;
var data = {};
data[resourceName] = { password: password };
data[resourceName][identificationAttributeName] = identification;
return _this.makeRequest(data).then(function (response) {
run(null, resolve, response);
}, function (xhr) {
return run(null, reject, xhr.responseJSON || xhr.responseText);
});
Promise構造函數中的this是什麼? – guest271314
這是一個Ember對象。它包含函數和屬性 –
還沒有試過'Ember.js','run'做了什麼? 'run'實際上是否調用'resolve'? – guest271314