Ember - 扩展 {{component}} 助手

Ember - Extending {{component}} helper

我想做的是将一个字符串传递给 {{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属性,不过不带参数。

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

// 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)
     }
})

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

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

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

{{component (getComponentToRender ComponentType.Button)}}

这里是 working twiddle.