[英]Ember issue with setting attribute value
我有一個定義如下的Ember Route類;
export default Ember.Route.extend({
model: function() {
var compObj = {};
compObj.gridPara = this.get('gridPara');
return compObj;
},
gridPara: function() {
var self = this;
var returnObj = {};
returnObj.url = '/myService';
// setting some other returnObj attributes
var summaryObj = {
total: {
label: "Total 1",
value: "100"
},
additional: [{
label: 'Label 2',
value: 'val2'
}, {
label: 'Label 3',
value: 'val3'
}]
};
returnObj.summary = summaryObj;
return returnObj;
},
actions: {
dataLoaded: function(resp) {
// Here I get the service response and want to set (or overwrite) the summaryObj values
this.get('gridParams').summary.total.value = resp.numRows;
}
}
});
我的模板看起來像
{{my-grid params=this.gridPara dataLoaded="dataLoaded"}}
現在,我想在returnObj上設置“摘要”,我已經驗證我在dataLoaded回調中獲得了“ resp”。
但我嘗試執行以下操作時收到以下錯誤
this.get('gridParams').summary.total.value = resp.numRows;
未捕獲的錯誤:斷言失敗:您必須使用Ember.set()來將[object Object]的value
屬性設置為100
。
另外,我如何在summaryObj中設置/推送“其他”數組
由於錯誤狀態,您必須將set
用作值(假設您在某處定義了gridParams,則是Im?):
this.set('gridParams.summary.total.value', resp.numRows);
為了推送一個新的對象,請嘗試此:
var additional = this.get('gridParams.additional');
additional.push({label: ..., value: ....});
this.set('gridParams.additional', additional);
不確定100%,但可以嘗試一下:
this.get('gridParams.summary.total.value')
this.set('gridParams.summary.total.value',resp.numRows)
只需添加到@Remi答案中,最佳做法是使用
Ember.set('gridParams.summary.total.value', resp.numRows);
回答評論中的問題說您要在索引i
處更新additional
數組。
var updateItem = additional[i];
Ember.set(updateItem.propertyname,newValue)
//Here propertyname would be the property you want to update and new Value is the new value which you want to set to that property
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.