我必须在我的控制器[]上访问一个两级深的属性,但是唯一可以通过emberjs指南访问一个级别的属性.
model(params) {
params.paramMapping = {
page: "page",
perPage: "per_page",
total_pages: "pages"
};
return this.findPaged('contractf', params);
},
setupController(controller, model) {
model.forEach(function(item) {
item.set('sale_price', item.get('dealers_sched_id.sale_price'));
});
controller.set('content', model);
},
Run Code Online (Sandbox Code Playgroud)
上面,我的模型基本上是在contractf模型中以分页格式获取所有记录.然后我设置我的控制器并循环遍历所有这些模型并绑定一个sale_price属性,该属性进入它的关系,以使sale_price获得正确的模型关系.
现在在我的模板中,我有这个:
new_suggested_price: Ember.computed('selectedItems', 'selectedItems.[].sale_price', function() {
var ret = 0;
this.get('selectedItems').filterBy('car_used', 'N').forEach(function(contract){
var salePrice = contract.get('sale_price');
if (salePrice) {
ret += (salePrice*100);
}
});
return ret/100; // We *100/100, so we avoid a floating point calc error.
}),
Run Code Online (Sandbox Code Playgroud)
基本上只给我一个容易格式化的数字.正如您所看到的,它取决于selectedItems(基本上是模型,但是属性过滤).所以我必须进入每个模型项并找到sale_price我设置的属性,如果它改变,这个计算属性将更新.阅读Ember的指南,我做不到,selectedItems.[].dealers_sched_id.sale_price因为它只有一层深.
我认为在setupController上设置一个属性可以解决这个问题,但它似乎并不是因为我仍然得到NaN了sale_price …