Emberjs将select的值设置为记录

时间:2014-06-25 13:59:19

标签: javascript ember.js

我有一张与表格有关的记录。在文本输入中,将显示正确的值,但不会出现在选择中。这是一个JsBin:http://jsbin.com/pexolude/87/edit

模板

  <script type="text/x-handlebars" data-template-name="index">
  <div class="well">
    {{#em-form model=model.person}}
        {{em-input property="name" label="Name" placeholder="Please enter first name."}}

        {{em-select
            label="Category"
            property="category"
            content=model.category
            optionValuePath="content.id"
            optionLabelPath="content.name"
            prompt="--select--"}}      
    {{/em-form}}


    </div>
  </script>

记录

var person = {
  "person":
  {"id":1,"name":"Joe","bio":"some tex","category":{id:"2","name":"Drama"}}
};

P.S。即时通讯使用ember-forms,但它继承自ember的输入,选择等。

1 个答案:

答案 0 :(得分:1)

您可以在此处看到:Ember Ember.select get selected value您可以为selectionBinding添加em-select属性。

这样,您可以通过在应用程序中点击保存或您喜欢的方式来保存所选值。

示例:

<script type="text/x-handlebars" data-template-name="index">
  <div class="well">
    {{#em-form model=model.person}}
        {{em-input property="name" label="Name" placeholder="Please enter first name."}}

        {{em-select
            label="Category"
            property="category"
            content=model.category
            optionValuePath="content.id"
            optionLabelPath="content.name"
            selectionBinding="selectedCategory"
            prompt="--select--"}}      
    {{/em-form}}


    </div>
  </script>

在您的控制器中:

selectedContentType: null,
  selectedCategory:null,

  actions: {
    save: function () {
      var selectedCategory = this.get('selectedCategory');
      console.log(selectedCategory);
      // do saving stuff here
    }
  }