user3568719
user3568719

Reputation: 1056

Emberjs set select's value to a record

I have a record bounded to a form. In the text input the right value appears, but not in the select. Here is a JsBin: http://jsbin.com/pexolude/87/edit

template

  <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>

the record

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

p.s. im using ember-forms but it inherits from ember's input, select etc.

Upvotes: 1

Views: 131

Answers (1)

wp_user
wp_user

Reputation: 138

Like you can see here: Ember Ember.select get selected value you can add a selectionBinding attribute to your em-select.

That way you can save the selected value by hitting save or the way you like in your application.

Example:

<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>

In your controller:

selectedContentType: null,
  selectedCategory:null,

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

Upvotes: 1

Related Questions