2012-03-16 106 views
2

如何在頁面加載時爲選擇框設置默認選項?我試過在下面的例子中設置selectedPersonController的默認值,但無濟於事。難道我做錯了什麼?Ember.Select默認選項

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

回答

2

你不要在你的App.selectedPersonController設置一個特定的人。在這裏看到一個工作示例:http://jsfiddle.net/ZpTYV/

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

我不知道這是不是在你的例子一個錯字,但你也必須聲明App全球範圍內,因此它可以在車把上的模板進行訪問。

+0

對不起,只是一個錯字。這正是我所期待的。謝謝! – skinneejoe 2012-03-17 01:47:17