Search code examples
selectember.jsdefault

Ember.Select default option


How do I set the default option for a select box when the page loads? I've tried setting the default value for the selectedPersonController in the example below but to no avail. Am I doing something wrong?

var App = Ember.Application.create();

App.Person = Ember.Object.extend({
    id: null,
    firstName: null,
    lastName: null,

    fullName: function() {
        return this.get('firstName') + " " + this.get('lastName');
    }.property('firstName', 'lastName').cacheable()
});

App.selectedPersonController = Ember.Object.create({
    person: null
});

App.peopleController = Ember.ArrayController.create({
    content: [
        App.Person.create({id: 1, firstName: 'Yehuda', lastName: 'Katz'}),
        App.Person.create({id: 2, firstName: 'Tom', lastName: 'Dale'}),
        App.Person.create({id: 3, firstName: 'Peter', lastName: 'Wagenet'}),
        App.Person.create({id: 4, firstName: 'Erik', lastName: 'Bryn'})
    ]
});

Template:

{{view Ember.Select
       contentBinding="App.peopleController"
       selectionBinding="App.selectedPersonController.person"
       optionLabelPath="content.fullName"
       optionValuePath="content.id"}}

Solution

  • You don't set a specific person on your App.selectedPersonController. See a working example here: http://jsfiddle.net/ZpTYV/

    // set a default selected person
    App.selectedPersonController.set('person', App.peopleController.objectAt(1));
    

    I don't know if this is a typo in your example, but you also have to declare App globally, so it can be accessed in the Handlebars template.