spullen
spullen

Reputation: 3317

populating select box using emberjs select view

I working on a toy app to learn ember. I have recipes that belong to styles. A style just consists of a name attribute (and the implicit id as well). I am having trouble setting up a select field in the new template to select which style the recipe is.

Here's what I currently have for my routes, controllers and templates:

Routes:

App.RecipesNewRoute = Ember.Route.extend({
  model: function() {
    return App.Recipe.createRecord({
      title: '',
      description: '',
      instructions: ''
    });
  },
  setupController: function(controller, model) {
    controller.set('content', model);
  }
});

Controllers:

App.RecipesController = Ember.ArrayController.extend({});

App.RecipesNewController = Ember.ObjectController.extend({
  create: function() {
    this.transitionToRoute('recipes.show', this.content);
  },

  cancel: function() {
    this.content.deleteRecord();
    this.transitionToRoute('recipes.index');
  },

  buttonTitle: 'Add Beer Recipe'
});

Templates:

recipes/new and form partial:

<script type="text/x-handlebars" data-template-name="recipes/new">
  {{ partial 'recipes/form' }}
</script>

<script type="text/x-handlebars" data-template-name="recipes/_form">
  {{ title }}
  <form>
    <fieldset>
      <div>
        <label {{bindAttr for="titleField.elementId"}}>Title</label>
        {{view Ember.TextField valueBinding='title' name='title' viewName='titleField'}}
      </div>
      <div>
        <label>Style</label>
        {{view Ember.Select valueBinding='style' name='style' viewName='styleField' contentBinding='App.Style.find()'}}
      </div>
      <div>
        <label {{bindAttr for='descriptionField.elementId'}}>Description</label>
        {{view Ember.TextArea valueBinding='description' name='description' viewName='descriptionField'}}
      </div>
      <div>
        <label {{bindAttr for='instructionsField.elementId'}}>Instructions</label>
        {{view Ember.TextArea valueBinding='instructions' name='instructions' viewName='instructionsField'}}
      </div>
      <a href='#' {{action create target='controller'}}>{{buttonTitle}}</a>
    </fieldset>
  </form>

  <a href='#' {{action cancel target='controller'}}>Cancel</a>
</script>

The main takeaway being: {{view Ember.Select valueBinding='style' name='style' viewName='styleField' contentBinding='App.Style.find()'}}

I've also tried setting up a styles variable in the controller: In RecipesNewController: styles: App.Style.find() and in the view {{view Ember.Select valueBinding='style' name='style' viewName='styleField' contentBinding='styles' optionValuePath='styles.id' optionLabelPath='styles.name'}}

as well as in the route's setupController: controller.set('styles', App.Style.find()) (with the same view code as with setting it in the controller).

Any help would be great, I'm sure it's something simple.

Thanks

Update

So I think I've almost got it.

In my RecipesNewRoute's setupController I have controller.set('styles', App.Style.find());. Then in my view I have {{view Ember.Select valueBinding='style' name='style' viewName='styleField' contentBinding='controller.styles' contentValuePath='content.id' contentLabelPath='content.name'}}. Now my problem is I have the select box being populated, except the label and value are set to <App.Style:ember407:100> instead of the id and the name.

Upvotes: 1

Views: 2477

Answers (1)

nrion
nrion

Reputation: 248

The Ember Select uses optionValuePath and optionLabelPath, and it appears you are using contentValuePath and contentLabelPath.

An example jsFiddle here: http://jsfiddle.net/nrionfx/BJwLR/2/

From the ember docs: http://emberjs.com/api/classes/Ember.Select.html

{{view Ember.Select
       contentBinding="App.programmers"
       optionValuePath="content.id"
       optionLabelPath="content.firstName"}}

Upvotes: 6

Related Questions