我正在为Ember
项目使用以下库:
DEBUG: -------------------------------
DEBUG: Ember.VERSION : 1.0.0
DEBUG: Handlebars.VERSION : 1.0.0
DEBUG: jQuery.VERSION : 2.0.2
DEBUG: -------------------------------
我在为Handlebars
应用程序拼凑Ember
模板时发现了一些奇怪的(?)行为,因为我想在{{with}}
块帮助器中创建一个CSS类绑定。看来,这个似乎不能按预期工作:
...
{{#with controller.currentData}}
<div class="mydata-container" {{bind-attr class="this.hasError:error:ok"}}>
{{this.foo}} - {{this.bar}}
</div>
{{/with}}
...
<div/>
将始终显示类似data-bindattr-666=666
(数字当然在增加),但这些类永远不会被“注入”。 (如果我省略this
关键字,它也没有区别,这也适用于显示数据)。如果我尝试{{log this.hasError}}
,我会得到等待的结果(true:false
)。
如果我在没有{{with}}
助手的情况下编写模板,请执行以下操作:
...
<div class="mydata-container" {{bind-attr class="controller.currentData.hasError:error:ok"}}>
{{controller.currentData.foo}} - {{controller.currentData.bar}}
</div>
...
它按预期工作。
这是Ember
中的已知问题/错误吗?
答案 0 :(得分:2)
我认为您的问题不在于{{#with}}
视图助手。但是因为您在同一时间声明了{{bind-attr class=...}}
和class="mydata-container"
html属性。
如果需要使用静态类,则必须在其前面添加冒号。例如{{bind-attr class =“:static-class other-dynamic-classes”}}。这也记录在案here。
您的更新代码如下:
{{#with controller.currentData}}
<div {{bind-attr class=":mydata-container this.hasError:error:ok"}}>
{{this.foo}} - {{this.bar}}
</div>
{{/with}}
我希望它有所帮助