2

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

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

{{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 属性会显示正确的图标

4

2 回答 2

1

最后很容易:

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

然后在控制器中:

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

这样,Select 将以默认值“2”开始;如果要设置默认值不是按值而是按内容,则需要使用 selectionBinding 而不是 valueBinding

于 2014-02-15T17:59:47.377 回答
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
});
于 2014-02-15T01:33:44.063 回答