2

Ember.TextFieldボタンが送信された後、から入力値を取得する最善の解決策は何ですか?

メッセージを送信するためのシンプルなアプリがあるとします。ユーザーがメッセージを入力する入力フォームを表すビューを指定しました。

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>

基本的に、必要なものは 1 つだけです。ユーザーがボタンをクリックすると、(submit 関数またはアプリ内の他の場所で) 通知を受ける必要があり、 から値を取得する必要がありますTextField。どうすればそれを達成できますか?

4

1 に答える 1

10

たとえば、テキスト フィールドの値をビューのメッセージ プロパティにバインドできます。

おそらく他の解決策があるかもしれませんが、これは非常に簡単だと思います: 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'));
    }
  }
});
于 2012-08-16T12:21:46.207 に答える