2013-02-17 39 views
3

我正在使用ember.select。我試圖動態設置價值。但它不適合我。我附上我的示例代碼。如何使用Ember.Select?我如何設置默認選擇的項目?

幫我解決這個問題。

這是我的車把模板 HBS: -

{{view Ember.Select 
    contentBinding="dropDown" 
    optionValuePath="content.id" 
    optionLabelPath="content.value" 
    selectionBinding="obj3" //This is the object am trying to set dynamically. I defined my object in below 
}} 


//View Model - This is my view model. This model gives the dropdown list 
App.MyModel = Em.Object.extend({ 
    id:{ 
     }, 
    dropDown:[ 
     {id:"1",value:"test1"}, 
     {id:"2",value:"test2"}, 
     {id:"3",value:"test3"} 
    ] 
}); 

// Model - Here is my data model. I want to update obj3 with dropdown list value. 
DataModel = Em.Object.extend({ 
    obj1:"", 
    obj2:"", 
    obj3:null 
}); 

// Create obj. Initially am trying to set default value for my dropdown. 

var dataModel = DataModel.create({ 
    obj1:"value1", 
    obj2:"value1", 
    obj3:{id:"1",value:"test1"} 
}) 

回答

1

你必須在你的應用程序當中保持選擇的值,並設定Em.SelectvalueBinding到路徑,例如:

window.App = Em.Application.create() 

App.ApplicationRoute = Em.Route.extend({ 
    model: function() { 
     return [ 
      {id: 1, text: "ein"}, 
      {id: 2, text: "zwei"}, 
      {id: 3, text: "polizei"} 
     ]; 
    } 
}); 

App.ApplicationController = Em.ObjectController.extend({ 
    selected: 2 // will select "zwei" 
}); 

在上面的ApplicationController中,我創建了一個屬性selected,用於保持要在組合中動態選擇的對象的id,然後在我的temp後期我設置了綁定,如下所示:

<script type="text/x-handlebars"> 
    {{view Ember.Select contentBinding="content" 
      optionLabelPath="content.text" 
      optionValuePath="content.id" 
      valueBinding="controller.selected"}} 
</script> 

這裏你可以看到一個工作小提琴http://jsfiddle.net/schawaska/zfVP8/

相關問題