将模型放在控制器emberjs中 [英] Getting the model inside a controller emberjs
问题描述
我的想法是创建一个控制器并在其上设置一个属性。然后在每个对象上抓取模型循环,并添加到对象总价格的预定义属性。如下所示:
导出默认值Ember.Controller.extend({
orderTotal:0,
getOrderTotal:function(){
var model = this.get('model');
model.forEach(function(c){
var order_total = this .get('orderTotal')* c.total_price;
this.set('orderTotal',order_total)
});
}
});
问题是我无法得到模型。如何访问控制器中的模型?
模型可能异步加载,因此您将不得不使用。 (...)等待模型加载。
this.get('model')then(function数据){...使用`data`创建和)};
尽管我将使用计算属性来创建可能改变的东西:
allPrices:Ember.computed.mapBy('model','price'),
totalPrice:Ember.computed.sum allPrices')
http://emberjs.jsbin.com/wiwiqulozi/1/
I need to get the grand total price of all the objects returned from a JSON API.
my idea is to create a controller and set a property on it. Then grab the model loop over each object and add to the predefined property that objects total price. Something like this:
export default Ember.Controller.extend({
orderTotal: 0,
getOrderTotal: function(){
var model = this.get('model');
model.forEach(function(c){
var order_total = this.get('orderTotal') * c.total_price;
this.set('orderTotal', order_total)
});
}
});
problem is I can't get the model. How can I access the model inside a controller?
The model is probably loaded asynchronously, therefore you will have to use .then(...) to wait for the model being loaded.
this.get('model').then(function(data) { ... create sum using `data` });
Though I would use computed properties to create the sum of something that can potentially change:
allPrices: Ember.computed.mapBy('model', 'price'),
totalPrice: Ember.computed.sum('allPrices')
http://emberjs.jsbin.com/wiwiqulozi/1/
这篇关于将模型放在控制器emberjs中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!