Ember。选择默认选项

时间:2012-03-16 18:31:09

标签: select default option ember.js

如何在页面加载时为选择框设置默认选项?我已尝试在下面的示例中为selectedPersonController设置默认值,但无济于事。我做错了吗?

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'})
    ]
});

模板:

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

1 个答案:

答案 0 :(得分:2)

您未在App.selectedPersonController上设置特定人员。请在此处查看工作示例:http://jsfiddle.net/ZpTYV/

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

我不知道这是你的例子中的拼写错误,但你还必须全局声明App,因此可以在Handlebars模板中访问它。