javascript - 我该如何使用 Ember.Select ?如何设置默认选择的项目?

标签 javascript html ember.js

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

帮我解决这个问题。

这是我的 Handlebar 模板 血红蛋白:-

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

最佳答案

您必须将选定的值保留在应用程序的某个位置,并将 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组合,然后在我的模板中按如下方式设置绑定(bind):

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

你可以在这里看到一个工作的 fiddle http://jsfiddle.net/schawaska/zfVP8/

关于javascript - 我该如何使用 Ember.Select ?如何设置默认选择的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14919218/

相关文章:

javascript - 使 div 适合内容的大小而不重叠文本?

php - CKEditor 获取选定的编辑器

javascript - 单击链接后让 Nightmare 等待下一页加载

javascript - 无法初始化动态构建的物化轮播

jquery-ui - 使用 Ember.js 和 jQuery.ui 的可排序列表

ember.js - 如何在单元测试期间 stub 计算属性?

javascript - JQuery 验证不适用于 ajax post

html - 我想在鼠标结束时更改颜色并在鼠标离开时返回它(html)?

html - 为什么我的背景图片拒绝显示?

javascript - 将 Ember.js 与 Raphael.js 一起使用 : How to associate Ember objects with Raphael objects?