【问题标题】:Ember - Extending {{component}} helperEmber - 扩展 {{component}} 助手
【发布时间】:2017-04-12 21:29:02
【问题描述】:

我想要做的是将一个字符串传递给{{component}} 助手,但我想更好地控制它在运行时传入的字符串。

例如

// getComponentToRender will get the current UI theme in the app, 
// determine whether it's running on the mobile mode, then fetches the 
// appropriate name of a component to render based on the type of the
// component. Just an example.
{{component getComponentToRender(ComponentType.Button)}}

我对此进行了调查,结果证明这是不可能的(否则,请纠正我)。当然,还有computed 属性,但它不带参数。

接下来,我研究了扩展助手。 This stackoverflow 问题显示您可以扩展一个,但我找不到 component 助手在 Ember 中的位置。似乎也没有关于扩展现有助手的任何问题/文档。这就是我要用这种方法做的事情。

// I can't find where the {{component}} helper is located.
import Component from './???'

export default Component.extend({
     compute(componentType, hash) {
         let componentName = getComponentToRender(componentType);
         this._super(componentName, hash)
     }
})

我错过了什么?如果有人能把我引向正确的方向,那就太好了。

【问题讨论】:

  • 也许结帐flexi
  • Flexi 是一个 UI 框架。我的问题与此无关。
  • Flexi 正在解决您想要的问题:如何为同一个组件设置多个布局。所以也许它可以帮助你检查他们是如何解决这个问题的。只是想指出你那里,对不起。我认为这对你来说会很有趣。

标签: ember.js ember-cli


【解决方案1】:

从模板调用函数的唯一方法是编写帮助程序。实际上,您的参考 StackOverflow 问题也提到了助手。 Writing an helper 会解决你的问题。

您不需要扩展 component 助手。你需要编写它。如下:

{{component (getComponentToRender ComponentType.Button)}}

这里是working twiddle

【讨论】:

  • 感谢您的回答。我还没试过,因为我出去了,但我会看看。为了回答您的问题,我可以针对不同的目的为同一个应用设置多个品牌。所以在产品 X 中,它可能是红色的,但产品 Y 可能是蓝色的。它们是不同的产品,但大多使用相同的代码。好吧,这只是一个例子。我不是真的在做那个自动取款机。
  • 我尝试了您的答案,但我认为它不起作用。什么都没有被渲染。请在这个旋转中看到它。 ember-twiddle.com/…
  • 你是绝对正确的。我已经确定 params 不是一个数组(甚至认为它有一个's'!)。它现在按预期工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多