灰烬选择设置默认值

时间:2014-02-15 01:29:14

标签: ember.js ember.select

即使阅读了很多文档,我仍然无法完成这个简单的任务;

我的模板中有一个选择视图:

{{view Ember.Select id="id_diff" contentBinding="difficulties" optionValuePath="content.id" optionLabelPath="content.title" valueBinding="selectedDifficulty"}}

此选择已正确填充此数据:     {id:1,title:easy} {id:2,title:medium} {id:3,title:hard}

我只想将默认值设置为“medium”而不是easy,这是现在的默认值;我怎么能这样做?

控制器:

Gmcontrolpanel.InsertActivityController = Em.Controller.extend({
difficulties: function() {
    var difficulties = [];
    difficulties = this.get('store').find('difficulty');
    return difficulties;
}.property(),
changeDifImg: function() {
    jQuery('.diff-icon').animate({'opacity': 0}, 300);
    jQuery('#diff-img' + jQuery('#id_diff').val()).animate({'opacity': 1}, 300);    
}.observes('selectedDifficulty'),
startValue: null
});

当用户更改选择中的值时,ChangeDifImg属性用于更改关联的难度图标;

(在模板中:

{{#each difficulty in difficulties}}
        <img class="diff-icon" id="diff-img{{unbound difficulty.id}}" src="{{unbound "difficulty.link"}}" style="opacity: 0" />
        {{/each}}

这为每一个难度创造了一个影像,并将它们全部隐形;然后,当用户在Select中选择一个值时,changeDifImg属性会显示正确的图标

2 个答案:

答案 0 :(得分:1)

最后很容易:

{{view Ember.Select contentBinding="controller.difficulty" optionValuePath="content.id" optionLabelPath="content.title" valueBinding="idDiff"}}

然后在控制器中:

App.DifficultyController = Ember.Controller.extend({
    idDiff: '2'
});

使用此选项将以默认值'2'开始;如果要设置默认值而不是按值设置内容,则需要使用selectionBinding而不是valueBinding

答案 1 :(得分:0)

指定selectionBinding:

{{view Ember.Select id="id_diff" 
    contentBinding="difficulties" 
    optionValuePath="content.id" 
    optionLabelPath="content.title" 
    valueBinding="selectedDifficulty" 
    selectionBinding="default"}}

并对您的控制器进行以下更改:

Gmcontrolpanel.InsertActivityController = Em.Controller.extend({
   difficulties: function() {
      var difficulties = [];
      difficulties = this.get('store').find('difficulty');
      return difficulties;
}.property(),
default: function() {
   return this.get('difficulties').first();
}.observes('difficulties.@each'),
changeDifImg: function() {
    jQuery('.diff-icon').animate({'opacity': 0}, 300);
    jQuery('#diff-img' + jQuery('#id_diff').val()).animate({'opacity': 1}, 300);    
}.observes('selectedDifficulty'),
startValue: null
});