2012-08-16 23 views
2

在提交按钮后,从Ember.TextField获取输入值的最佳解决方案是什么?如何在Ember.js应用程序中获取输入值?

假设我有简单的应用程序发送消息。我指定表示用户在其中输入他的消息输入形式的视图:

App.TextView = Ember.View.extend({ 
    templateName: 'text', 

    submit: function(event) { 
     console.log(event); 
    } 
}); 

然后,有此视图把手模板:

<script type="text/x-handlebars" data-template-name="text"> 
    <h1>Send the message:</h1> 
    <div class="send_form"> 
     {{view Ember.TextField}} 
     <button type="submit">Done</button> 
    </div> 
</script> 

基本上,我需要一两件事。当用户点击按钮时,我需要得到通知(在提交功能或我的应用程序中的任何其他地方),我需要从TextField获取值。我怎样才能做到这一点?

回答

9

例如,您可以将文本字段的值绑定到视图中的消息属性。

或许有其他的解决办法,但我认为这很简单:http://jsfiddle.net/Sly7/vfaF3/

<script type="text/x-handlebars"> 
    {{view App.TextView}} 
</script> 

<script type="text/x-handlebars" data-template-name="text"> 
    <h1>Send the message:</h1> 
    <div class="send_form"> 
    {{view Ember.TextField value=view.message}} 
    <button type="submit" {{action "submit" target=view}}>Done</button> 
    </div> 
</script>​ 
App = Ember.Application.create({}); 

App.TextView = Ember.View.extend({ 
    templateName: 'text', 
    message: '', 

    actions: { 
    submit: function(event) { 
     console.log(this.get('message')); 
    } 
    } 
}); 
+0

感谢。然而,你的例子不适合我,因为我的应用程序使用路由器,并且操作被重定向到当前的路由器状态。我该如何处理? – 2012-08-16 13:18:07

+1

啊,看来'target =“view”'解决了它......谢谢,伙计! – 2012-08-16 13:19:08

+0

对不起,我没有预料到这一点;) – 2012-08-16 13:22:38

相关问题