Ember 的 registerBoundHelper 和车把块 [英] Ember's registerBoundHelper and handlebar blocks

查看:18
本文介绍了Ember 的 registerBoundHelper 和车把块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我从 here 中获取了is"帮助块并对其进行了修改使用 registerBoundHelper 通过 Ember 注册它的助手

So I have taken the 'is' helper block from here and modified it so that it register's it's helper through Ember using registerBoundHelper

我这样做的原因是因为我基本上需要一个基于把手的switch"语句.我的车把最终结果如下:

The reason I did that is because I basically need a handlebars based 'switch' statement. The end result in my handlebars is as follows:

{{#is MyProperty 1}}
    ...Do something here...
{{/is}}
{{#is MyProperty 2}}
    ...Do something here...
{{/is}}
{{#is MyProperty 3}}
    ...Do something here...
{{/is}}
{{#is MyProperty 4}}
    ...Do something here...
{{/is}}

is 语句只是在 'MyProperty 的 value 和一个常量之间做了一个简单的比较.

The is statement just does a simple comparison between 'MyProperty's value and a constant.

如果我不使用 'registerBoundHelper',则 MyProperty 将作为字符串文字 'MyProperty' 而不是 value 传递.

If I don't use 'registerBoundHelper' MyProperty gets passed through as the string literal 'MyProperty' and not it's value.

现在:这个逻辑在我实际运行时似乎有效

问题是 Ember 抛出以下错误:

The issue is that Ember throws the following error:

registerBoundHelper-generated helpers do not support use with Handlebars blocks.

我是否应该忽略此错误并继续进行,因为它似乎确实有效?或者我应该尝试重新设计逻辑以不使用块?

Should I ignore this error and just continue on because it does appear to be working? Or should I try to rework the logic to not use a block?

推荐答案

你不应该因为它不受支持而忽略它,如果 MyProperty 在渲染发生后改变它会中断.

You shouldn't ignore it because it isn't supported, if MyProperty were to change after the render has occurred it will break.

在 ember 车把中不支持条件助手(对于我们的许多人来说已经消亡).原因是核心团队希望将此逻辑作为计算属性而不是模板中的逻辑.

Conditional helpers aren't supported (to many of our demise) in ember handlebars. The reason being is the core team wants this logic as a computed property instead of logic in the template.

浏览器

App.IndexController = Em.ObjectController.extend({
  isPropertyOne: Em.computed.equal('myProperty', '1')
});

模板

  {{#if isPropertyOne}}
    any template stuff
    {{render 'something'}}
    {{someValue}}
  {{/if}}

这篇关于Ember 的 registerBoundHelper 和车把块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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