[英]Ember issue with setting attribute value
I have an Ember Route class defined as below; 我有一个定义如下的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 template looks like 我的模板看起来像
{{my-grid params=this.gridPara dataLoaded="dataLoaded"}}
Now I want to set the "summary" on returnObj I have verified that I get "resp" inside dataLoaded callback. 现在,我想在returnObj上设置“摘要”,我已经验证我在dataLoaded回调中获得了“ resp”。
But I get the following error when trying to do 但我尝试执行以下操作时收到以下错误
this.get('gridParams').summary.total.value = resp.numRows;
Uncaught Error: Assertion Failed: You must use Ember.set() to set the value
property (of [object Object]) to 100
. 未捕获的错误:断言失败:您必须使用Ember.set()来将[object Object]的
value
属性设置为100
。
Also how do I set/push for "additional" array inside summaryObj 另外,我如何在summaryObj中设置/推送“其他”数组
As the error states, you must use set
to the the value (Im assuming you have gridParams defined somewhere?): 由于错误状态,您必须将
set
用作值(假设您在某处定义了gridParams,则是Im?):
this.set('gridParams.summary.total.value', resp.numRows);
In order to push a new object, try this: 为了推送一个新的对象,请尝试此:
var additional = this.get('gridParams.additional');
additional.push({label: ..., value: ....});
this.set('gridParams.additional', additional);
not 100% sure, but give it a try: 不确定100%,但可以尝试一下:
this.get('gridParams.summary.total.value')
this.set('gridParams.summary.total.value',resp.numRows)
this.set('gridParams.summary.total.value',resp.numRows)
Just adding to @Remi answers ,the best practice would be to use 只需添加到@Remi答案中,最佳做法是使用
Ember.set('gridParams.summary.total.value', resp.numRows);
To answer the question in your comment Say you want to update additional
array at index i
.Just do 回答评论中的问题说您要在索引
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.