ember.js - 将条件类名放入助手中

标签 ember.js handlebars.js

如何将条件类名放入辅助/组件元素中?

像这样:我有 {{input}} 如果满足某些条件,则应该更改类名,我可以使用 if external 来做到这一点,

有办法制作这个衬垫吗?

{{#if model.bar.errors.name.length > 0}}
   {{input value=model.bar.name label='Bar Name' placeholder='The Name of Your Establishment' class=(model.bar.errors.name.length > 0 "color-" "blue")}}
{{#else}}
   {{input value=model.bar.name label='Bar Name' placeholder='The Name of Your Establishment' class="field-error"}}
{{/if}}

最佳答案

Ember 中的相关概念是类名绑定(bind)。

有多种方法可以将类或其他 html 属性的名称绑定(bind)到应用程序中的值:

1)模板中内联if语句:

<div class="{{if someVariable 'color-blue' 'not-color-blue}}">...</div>

2) 将 classNameBindings 属性传递给您的组件:

// htmlbars template
{{foo-bar  classNameBindings="isBlue:color-blue" model=model}}

//component.js
isBlue: Ember.computed(function(){
  return (this.get('model.color.name') === 'blue');
})  

3) 在你的 component.js 中(这是为组件设置默认类绑定(bind)的好方法——当与上面的 #2 结合使用时,模板中的类绑定(bind)将覆盖 component.js 中的类绑定(bind))文件):

//component.js
classNameBindings: ["isBlue:color-blue"],
isBlue: Ember.computed(function(){
  return (this.get('model.color.name') === 'blue');
}) 

选项 #2 和 #3 是最明智的方法。注意 classNameBindings 是一个数组。您可以使用此语法为元素指定任意数量的绑定(bind)。

文档:
http://guides.emberjs.com/v2.0.0/components/customizing-a-components-element/

关于ember.js - 将条件类名放入助手中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32904946/

相关文章:

node.js - Ember Cli 不使用 broccoli-emblem-compiler 进行编译

javascript - Ember 使用每个

javascript - 在 Ember.js 中使用 itemController 时可以有自定义 View 吗?

javascript - 使用 {{render}} 渲染时的循环计数器

javascript - 使用helper 在handlebars.js 中创建链接

ember.js - 在 Ember.js 中处理事件的最佳方式是什么?

javascript - 右键单击和左键单击选项需要靠近单击行

ember.js - Handlebars 错误 : Could not find property 'query-params' although Feature activated

node.js - 设置 Handlebars 助手以快速返回每个请求的特定值

reactjs - React 中的 Handlebars 模板