ember.js选择默认选项

niknxzdl  于 2022-09-28  发布在  其他
关注(0)|答案(1)|浏览(131)

如何在页面加载时为选择框设置默认选项?在下面的示例中,我尝试设置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"}}
o2g1uqev

o2g1uqev1#

您没有在App.selectedPersonController上设置特定人员。请参见以下工作示例:http://jsfiddle.net/ZpTYV/

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

我不知道这是否是您的示例中的输入错误,但您还必须全局声明App,以便可以在Handlebars模板中访问它。

相关问题