如何作为jQuery元素访问Ember输入?

cav*_*neb 3 ember.js

我在Ember中有一个简单的表单,它提交给服务器并返回响应.在失败的响应中,我想重新关注输入字段.有没有办法使用值绑定访问我的控制器中的字段?

这是我的jsbin作为例子:

http://jsbin.com/umodir/1/edit

Rya*_*nce 5

http://jsbin.com/efatut/2/edit

您的服务器应返回在控制器上设置您可以观察到的属性的内容.我做得很简单,称之为"错误".

var App = Ember.Application.create();

App.ApplicationController = Ember.ObjectController.extend({
  error: false,
  submit: function() {
    alert('I want to set a focus on the email input field now...');
    this.set('error', true);
  }  
});

App.ApplicationView = Ember.View.Extend({
  focusEmail: function() {
    if (this.get('controller.error')) {
      this.$('input[type=text]').focus();
    }
  }.observes('controller.error')
});
Run Code Online (Sandbox Code Playgroud)

如果你想得到真正的想象,你可以使用Ember.Component {{eb-input focuson="error"}},当控制器的"错误"属性改变时,它会自动聚焦.