Ember从模型中的Controller获取属性 [英] Ember Getting property from Controller in Model

查看:323
本文介绍了Ember从模型中的Controller获取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型的计算属性,为了计算我需要控制器的属性(而不是控制模型的属性)。
我知道有需要:,但这只是在

a控制器级别。

如何获得Ember中的一个控制器的属性不在管理型号的控制器中?

I have a computed property on a model and in order to compute that I need a property from a controller (not the one that is controlling the model). I know there is needs: but this is just on
a controller level.
How could I get a property in Ember from a controller other than the one that is managing the model?

我正在尝试像[问这个问题] [1]的人做一些格式化,但是我没有成功建议那里。

所以我尝试使用计算属性对模型进行格式化,但是要计算该属性,我需要控制器中的另一个属性。

I'm trying to do some formatting like the person that [asked this question][1] but I didn't succeed what has been suggested there.
So I try to do the formatting on the model with a computed property, but to calculate that property I need another property from a controller.

任何帮助都是不胜感激!谢谢!

Any help is greatly appreciated! Thanks!

注意:我正在使用EmberData来管理模型。

Note: I'm using EmberData to manage the model.

编辑: / strong>

为了澄清我想要做什么,我已经设置了一个示例,显示问题

一般方式:示例应用程序允许您输入数字,存储它们,

并将其显示在列表中。您还可以输入转换因子,它不会改变模型数据本身,而是模板上的演示文稿。说,你输入的数字2,2被保存在模型中,但是当它在列表中显示时,它会以前面输入的转换因子格式化,模板中会显示计算的值。问题是我要格式化的值存储在不同的控制器上。这是我迄今为止所尝试的:


In order to clarify what I'm trying to do I have set up an example that shows the problem
in a general way: The example application lets you input numbers, store them,
and show them in a list. You can also input a "conversion factor" which doesn't change the model data itself but the presentation on the template. Say, you input the number 2, 2 gets saved on the model but when it is shown in the list it gets "formatted" with the conversion factor you entered previously and the calculated value is shown in the template. The problem is that the value with which I want to format is stored on a different controller. Here's what I have tried so far:


  • #1方法:

    ArrayController上的计算属性 - 使用需要:控制器中的遍历并获取值

    - > jsfiddle

    我遇到的问题:

    ArrayController似乎中断了,模板表示完全没有存储记录(注意:该示例使用本地存储,因此创建一些记录并取消对ArrayController上的计算属性的注释,您将看到它原先按预期运行,并显示您输入的记录)。

  • #1 Approach:
    Computed Property on the ArrayController - using needs: in the controller to traverse and get the value
    -->jsfiddle
    Problems I have encountered:
    The ArrayController seems to break and the template renders as if there are no stored records at all (Note: the example uses local storage, so create some records and uncomment the computed property on the ArrayController and you'll see it works originally as expected and shows the records you entered).

#2方法:

模型本身计算的财产

- > jsfiddle

我遇到的问题:

我不知道如何从模型中获取一个控制器的财产

#2 Approach:
Computed Property on model itself
-->jsfiddle
Problems I have encountered:
I have no idea how I can get a property from a controller while beeing inside the model

如果有人有一个想法如何解决这个问题,或者可以指出我正确的方向将是巨大的。你的帮助真的很感激!谢谢你的时间!

If anyone has an idea how to solve this or can point me into the right direction would be great. Your help is really appreciated! Thanks for your time!

推荐答案

我的工作:我使用了#3方法(在我的问题中概述)。

I got it working: I've used the #3 Approach (outlined in my question).


  • 我正在使用计算属性将另一个控制器的值代理到绑定到我想要的模板的控制器显示格式化的值

  • 我创建了一个名为转换的handlebars帮助器,将执行格式化。句柄帮助器接受两个参数:我从控制器代理的值(又来自不同的控制器)和来自模型的值。

  • I'm using a computed property to proxy the value from the other controller to the controller that is bound to the template where I want to display the formatted value
  • I created a handlebars helper called converted that will do the formatting. The handlebars helper accepts two parameters: the value that I proxy from the controller (which in turn comes from a different controller) and the value from the model.

以前没有工作的事情是在使用助手的模板中,我会在所有项目上获得NaN,而不是格式化的输出。

解决问题的方法是而不是通过模板中的模型浏览:

The thing that didn't work previously was that in the template when using the helper I would get "NaN" on all items instead of the formatted output.
What solved the problem was that instead of lopping through the model in the template with:

    {{#each controller}}
    <tr>
        <td>{{converted amount conversionFactor}}</td>
    </tr>
    {{else}}
    <tr>
        <td>No amounts here yet</td>
    </tr>
    {{/each}}

我将其更改为:

    {{#each item in controller}}
    <tr>
        <td>{{converted item.amount conversionFactor}}</td>
    </tr>
    {{else}}
    <tr>
        <td>No amounts here yet</td>
    </tr>
    {{/each}}

它的工作原理完美!

以下是 jsfiddle

这篇关于Ember从模型中的Controller获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆