在Ember中获取模型控制器

时间:2022-03-02 21:06:03

If I have a reference to a model in Ember, how do I access its controller? If you're in the controller and want to access the model it's as simple as .get("model") but the reverse does not seem to hold when you need the controller.

如果我在Ember中引用模型,我该如何访问其控制器?如果你在控制器中并且想要访问模型,它就像.get(“model”)一样简单,但是当你需要控制器时反向似乎并不成立。

1 个解决方案

#1


1  

You can calculate the combined totals in the parent model, like so:

您可以计算父模型中的合计总数,如下所示:

total: function() {
    var amount = 0;
    this.get('years').forEach(function(item) {
        amount += item.get('total');
    });
    return amount;
}.property('years.@each.total')

Where "years" is the child model from the hasMany relationship. This will be dynamically updated any time any of the children change value, using the @each keyword.

其中“年”是来自hasMany关系的子模型。每当任何一个孩子使用@each关键字更改值时,都会动态更新。

See this full jsfiddle

看到这个完整的jsfiddle

#1


1  

You can calculate the combined totals in the parent model, like so:

您可以计算父模型中的合计总数,如下所示:

total: function() {
    var amount = 0;
    this.get('years').forEach(function(item) {
        amount += item.get('total');
    });
    return amount;
}.property('years.@each.total')

Where "years" is the child model from the hasMany relationship. This will be dynamically updated any time any of the children change value, using the @each keyword.

其中“年”是来自hasMany关系的子模型。每当任何一个孩子使用@each关键字更改值时,都会动态更新。

See this full jsfiddle

看到这个完整的jsfiddle