Ember.js 在余烬更新disabledBinding属性之前激发的选择字段上的focus()

Ember.js 在余烬更新disabledBinding属性之前激发的选择字段上的focus(),ember.js,Ember.js,在我的应用程序中,我有一个扩展的Em。选择视图: App.SelectField = Em.Select.extend({ attributeBindings: ['disabled'], actions: { setFocus: function(){ this.$().focus(); } } }); 在我的车把模板中使用: {{view App.SelectField viewName="cbAdd

在我的应用程序中,我有一个扩展的
Em。选择
视图:

App.SelectField = Em.Select.extend({
    attributeBindings: ['disabled'],
    actions: {
        setFocus: function(){
            this.$().focus();
        }
    }
});
在我的车把模板中使用:

{{view App.SelectField 
    viewName="cbAddressType"
    disabledBinding='getIsDisabled'
    contentBinding="controllers.addressTypes"
    optionValuePath="content.id"
    optionLabelPath="content.addressTypeName"
    valueBinding="getSetAddressType"}}
<button {{action "editRecord" view.cbAddressType}}>Edit</button>
视图的
禁用绑定
绑定到视图的控制器属性
getIsDisabled
。此属性更改时,“选择视图”处于启用或禁用状态。这个很好用

但是,我对模板也有一个操作:

{{view App.SelectField 
    viewName="cbAddressType"
    disabledBinding='getIsDisabled'
    contentBinding="controllers.addressTypes"
    optionValuePath="content.id"
    optionLabelPath="content.addressTypeName"
    valueBinding="getSetAddressType"}}
<button {{action "editRecord" view.cbAddressType}}>Edit</button>
它将
setFocus
操作发送到上面的
SelectField
,然后在select控件上运行
focus()
方法

问题是在Select上的
disabledBinding
属性更新为控制器中绑定属性的更改值之前,会触发
focus()
。控件在启用之前无法接收焦点。我在select.disabled[wait]循环时尝试了一个
,但这只是导致了一个无限循环,因为视图似乎在调用
setFocus
后才刷新-它们不是异步运行的

我还注意到,在使用
readonlyBinding
而不是
disabledbbinding
Ember.TextField
上,这种方法工作得很好:在触发focus()事件之前,文本字段的
readonly
属性被删除


感谢您的建议。

未经测试,但可以正常工作。在你的
应用程序中,选择field
使用:

setFocus: function(){
  Ember.run.schedule('afterRender', this, function() {
    this.$().focus();
  });
}

谢谢你的回复。添加了'Ember.run.schedule('afterRender',this,function(){$(this.elementId.focus();});'但是.focus()仍在Select删除其禁用属性之前被触发。还有什么好主意吗?哦,对不起,没问题。在我的一次解决方案尝试中,我在模板中加入了一个#if,根据变量isEditing state显示不同的选择-因此元素在呈现之前和之后发生了变化-因此它试图将()聚焦在不再存在的控件上!所以,谢谢,这很有效。虽然我为什么需要为Select而不是输入元素这样做,但我不知道!余烬有一些队列,渲染视图后会调用
afterRender
。我不知道为什么这个问题只是发生在选择。。。如果我的回答解决了你的问题,请接受。否则,请编写您的aproach,以便具有相同问题的用户可以获得一些参考。谢谢