ember.js `afterModel` 事件处理程序更改未反映在模板中
ember.js `afterModel` event handler changes not reflected in Template
我有一条路线,我在其中使用 Ajax(不是 Ember 数据)从服务器中提取记录 costcentre
。该记录旨在为后续编辑填充模板。
在提取之前,在 beforeModel
中,使用 createRecord
创建了一个空的 costcentre
。 model
处理完成后,在 afterModel
中,返回的数据用于填充 Data Store 中的 costcentre
对象。
数据提取成功,在调试器中可以看到本地存储的 DS 对象的更新有效,但在模板中看不到更改。
如何获取模板以填充从服务器返回的数据?
在我的路线中:
beforeModel: function(transition) {
this.set('ccToEdit', this.store.createRecord('costcentre'));
},
model(params) {
return getCCByCCIdent( this.urlbase,
this.currentMOP.currentMOP,
ENV.APP.MATClientCode,
params.cceIdentifier_to_edit);
},
afterModel(ccs, transition) {
//I'm testing this with an API end point that returns a
//list but there will only ever be one item in the list
this.ccToEdit.setProperties(ccs[0]);
},
getCCByCCIdent
看起来像这样:
export const getCCByCCIdent = function(urlbase, currentMOP, clientCode, targetCostCentreIdent) {
return new Promise(function (resolve, reject) {
if (targetCostCentreIdent.length == 0)
{
resolve([])
}
else
{
var theUrl = `${urlbase}/costcentres/${currentMOP}/${clientCode}/${targetCostCentreIdent}`;
$.ajax({
type: 'GET',
url: theUrl,
success: function (response) {
resolve(response);
},
error: function (request, textStatus, error) {
reject(error);
}
});
}
})
}
最简单的方法是对 return 从您的 Ajax 调用中编辑的承诺执行 then()
,然后设置适当的值,然后 return 你的模特:
model(params) {
return getCCByCCIdent(
this.urlbase,
this.currentMOP.currentMOP,
ENV.
params.cceIdentifier_to_edit
).then(ccs => {
let costCentre = this.store.createRecord('costcentre');
costCentre.setProperties(ccs[0]);
return costCentre;
});
},
我有一条路线,我在其中使用 Ajax(不是 Ember 数据)从服务器中提取记录 costcentre
。该记录旨在为后续编辑填充模板。
在提取之前,在 beforeModel
中,使用 createRecord
创建了一个空的 costcentre
。 model
处理完成后,在 afterModel
中,返回的数据用于填充 Data Store 中的 costcentre
对象。
数据提取成功,在调试器中可以看到本地存储的 DS 对象的更新有效,但在模板中看不到更改。
如何获取模板以填充从服务器返回的数据?
在我的路线中:
beforeModel: function(transition) {
this.set('ccToEdit', this.store.createRecord('costcentre'));
},
model(params) {
return getCCByCCIdent( this.urlbase,
this.currentMOP.currentMOP,
ENV.APP.MATClientCode,
params.cceIdentifier_to_edit);
},
afterModel(ccs, transition) {
//I'm testing this with an API end point that returns a
//list but there will only ever be one item in the list
this.ccToEdit.setProperties(ccs[0]);
},
getCCByCCIdent
看起来像这样:
export const getCCByCCIdent = function(urlbase, currentMOP, clientCode, targetCostCentreIdent) {
return new Promise(function (resolve, reject) {
if (targetCostCentreIdent.length == 0)
{
resolve([])
}
else
{
var theUrl = `${urlbase}/costcentres/${currentMOP}/${clientCode}/${targetCostCentreIdent}`;
$.ajax({
type: 'GET',
url: theUrl,
success: function (response) {
resolve(response);
},
error: function (request, textStatus, error) {
reject(error);
}
});
}
})
}
最简单的方法是对 return 从您的 Ajax 调用中编辑的承诺执行 then()
,然后设置适当的值,然后 return 你的模特:
model(params) {
return getCCByCCIdent(
this.urlbase,
this.currentMOP.currentMOP,
ENV.
params.cceIdentifier_to_edit
).then(ccs => {
let costCentre = this.store.createRecord('costcentre');
costCentre.setProperties(ccs[0]);
return costCentre;
});
},