是否可以将条件语句或其他javascript作为参数传递到ember句柄中

Is it possible to pass conditionals or other javascript as arguments in ember handlebars?

本文关键字:参数传递 ember 句柄 javascript 其他 条件 语句 是否      更新时间:2024-01-15

我想把一个true/false语句传递给我的车把

{{Gd-text-input label="Specify" name="Specify" key="entry.810220554" hideIf="entry.18110 === "Client""}}

如果变量entry.18110设置为"Client

首先,将其添加到某个位置-

Handlebars.registerHelper('ifEqual', function (var1, var2, options) {
    if (var1=== var2) {
        return new Handlebars.SafeString(options.fn(this));
    }
    return new Handlebars.SafeString(options.inverse(this));
});

然后。。

{{#ifEqual entry.18110 "Client"}}
{{Gd-text-input label="Specify" name="Specify" key="entry.810220554" hideIf="true"}}
{{else}}
{{Gd-text-input label="Specify" name="Specify" key="entry.810220554" hideIf="false"}}
{{/if}}

这几乎是唯一的方法,因为handlerbars团队特别将大多数逻辑排除在模板之外,因为它通常不属于模板。这是有争议的,因为有时不允许简单的逻辑会使事情变得更加复杂。但是,这是非常变通的。

另一个答案不适用于Ember Handlebars,对于您的情况,您可以这样做。

http://emberjs.jsbin.com/agewuxAT/3/edit

组件

App.HideableCompComponent = Em.Component.extend({
  isHidden: function(){
    var prop = this.get('hideIfProperty');
    if(!prop) return false;
    // allow lazy comparison? up to you
    return this.get('content').get(prop) == this.get('hideIfValue');
  }.property('hideIfProperty', 'hideIfValue')
});

模板

<script type="text/x-handlebars" data-template-name="components/hideable-comp">
  {{#unless isHidden}}
    I am a component I am not hidden!
  {{else}}
    I am hidden
  {{/unless}}
</script>

使用

{{hideable-comp content=model hideIfProperty='length' hideIfValue=3}}