灰烬+ HTMLBars:“ boolean”绑定属性不是布尔值 [英] Ember + HTMLBars: "boolean" bound attributes are not booleans

查看:119
本文介绍了灰烬+ HTMLBars:“ boolean”绑定属性不是布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Ember 1.5 Handlebars应用程序迁移到当前稳定的Ember和HTMLBars,看来绑定控制器属性必须返回 disabled null 可以与禁用的 DOM属性一起正常使用。

I'm migrating an Ember 1.5 Handlebars app to current stable Ember and HTMLBars and it seems that a bound controller property must return "disabled" or null to work as expected with "disabled" DOM attributes.

<button disabled={{isDisabled}}> 

在把手中 isDisabled 属性是一个布尔值,一切都很好。

In Handlebars isDisabled property is a boolean and all is well.

在HTMLBars中似乎需要:

In HTMLBars it seems I need:

Ember.Controller.extend({
  isDisabled: function() {
    if(this.get('itemSelected')){
      return null;
    } else {
      return 'disabled';
    }
  }.property('itemSelected')
});

这是正确的吗?当然,这会带来一个问题,因为在应用程序的其余部分中,布尔属性应该是一个布尔值,因此要使它按预期工作,我需要添加一个附加的计算属性来驱动布尔值-设置为 string / null 值的ish DOM属性。

Is this correct? This presents a problem of course since a boolean property is expected to be, well, a boolean in the rest of the app, so to get this to work as expected I'll need to add an additional computed property to drive the "boolean-ish" DOM attribute with a "string"/null value set.

是否有人遇到过此问题,或者

Has anyone else encountered this, or the related issue with "checked"?

使用情况:

Ember 1.11.3 + HTMLBars

ember-cli 0.2.3

Using:
Ember 1.11.3 + HTMLBars
ember-cli 0.2.3

推荐答案

我通过使用绑定助手来为此提供了一个合理的解决方案。

I came up with a reasonable solution for this by using a bound helper.

// ../helpers/boolean-disabled.js

import Ember from 'ember';

export function booleanDisabled(params/*, hash*/) {
  var disabled = params[0];
  if(disabled) {
    return 'disabled';
  } else {
    return null;
  }
}

export default Ember.HTMLBars.makeBoundHelper(booleanDisabled);

然后在模板中

<button disabled="{{boolean-disabled itemSelected}}">

这篇关于灰烬+ HTMLBars:“ boolean”绑定属性不是布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆