目前我试图使用一个功能来观察ember模板(把手)中的控制器/组件的字段。
index.hbs
<div>
{{ input value=model.field1 }}
{{if hasFieldError('field1')}}
<span>This field is required</span>
{{/if}}
</div>
<div>
{{ input value=model.field2 }}
{{if hasFieldError('field2')}}
<span>This field is required</span>
{{/if}}
</div>
index.js
hasFieldError: function(val) {
return true if val is found in an array
}.observes('field1', 'field2'),
但这当然会返回构建错误:
{#if hasFieldError('compa ----------------------^ Expecting
'CLOSE_RAW_BLOCK', 'CLOSE', 'CLOSE_UNESCAPED', 'OPEN_SEXPR',
'CLOSE_SEXPR', 'ID', 'OPEN_BLOCK_PARAMS', 'STRING', 'NUMBER',
'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', 'SEP', got 'INVALID'
知道怎么做到这一点?
contains
的帮手:{{#if (contains array1 field1)}}
但是有人已经写过了。欢迎来到精彩的Ember社区!见https://github.com/DockYard/ember-composable-helpers#contains
只需用计算属性替换您的观察者函数:
hasFieldError: Ember.computed('field1', 'field2', function(val) {
return true if val is found in an array
}),