如何以一个jQuery元素的形式访问Ember输入? [英] How can I access an Ember input as a jQuery element?

查看:109
本文介绍了如何以一个jQuery元素的形式访问Ember输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我的jsbin作为一个例子:



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

解决方案

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



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

  var App = Ember.Application.create(); 

App.ApplicationController = Ember.ObjectController.extend({
error:false,
submit:function(){
alert('我想设置焦点在电子邮件输入栏现在...');
this.set('error',true);
}
});

App.ApplicationView = Ember.View.Extend({
focusEmail:function(){
if(this.get('controller.error')){
这个$('input [type = text]')。focus();
}
} .observes('controller.error')
});

如果您想要真正想要使用Ember.Component,如 {{eb-input focuson =error}} ,当控制器的错误属性更改时,将自动聚焦。


I have a simple form in Ember which submits to a server and returns a response. On a failed response, I want to re-focus on the input field. Is there a way to access the field in my controller using the value binding?

Here's my jsbin as an example:

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

解决方案

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

Your server should return something that sets a property on your controller that you can observe. I made it pretty simple and called it "error".

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')
});

If you wanted to get really fancy you could use an Ember.Component like {{eb-input focuson="error"}} that would automatically focus when the controller's "error" property changed.

这篇关于如何以一个jQuery元素的形式访问Ember输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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