3

以下面的示例为例,如果未选中复选框,则禁用按钮并在选中时启用它:

车把:

<form {{action "register" on="submit"}}>
  {{input type="checkbox" checked=isAgreed}}
  {{input type="submit" value="Register" class="btn btn-primary" disabled=isNotAgreed}}
</form>

Javascript:

App.ApplicationController = Ember.ObjectController.extend({
  content:{
    isAgreed:false
  },
  isNotAgreed:function(){
    return !this.get('isAgreed');
  }.property('isAgreed'),
  actions:{
    register:function(){
      alert("registered");
    }
  }
});

如本JSBin所示。

我想知道是否有更清洁的方法来做到这一点。例如,删除isNotAgreedobservable 属性并在我的车把模板中使用一些东西,比如{{input type="submit" value="Register" disabled=!isAgreed}}. 有没有办法做到这一点?

4

2 回答 2

3

或者,您可以绑定一个当如下时为disabled真的类。isAgreedfalse

<button type="submit" {{bind-attr class=":btn :btn-primary isAgreed::disabled"}}>Register</button>  

由于您也在使用twitter-bootstrap,因此此类disabled将为按钮提供类似禁用的行为。

这是您的问题的简化

于 2013-09-09T15:21:38.800 回答
3

目前在车把模板的绑定属性中不存在 not 运算符!

您可以使用计算属性宏not来获得一些语法糖:

所以而不是:

isNotAgreed:function(){
  return !this.get('isAgreed');
}.property('isAgreed')

您可以使用:

isNotAgreed: Ember.computed.not('isAgreed')

您可以在此处查看所有可用的计算宏

我希望它有帮助

于 2013-09-09T15:03:26.053 回答