繁体   English   中英

我怎样才能使用Ember.Select? 如何设置默认选定项?

[英]How can I use Ember.Select ? How can I set default selected item?

我正在使用ember.select。 我试图动态设置值。 但这对我不起作用。 我附上了我的示例代码。

帮我解决这个问题。

这是我的Handlebar模板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"}
})   

您必须将所选值保留在应用程序中的某个位置,并将valueBindingEm.Select设置为该路径,例如:

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 ,然后在我的模板中,我按如下方式设置绑定:

<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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM