Ember。选择未正确设置默认值

时间:2012-04-04 15:40:37

标签: ember.js

我已经阅读了所有测试两次,文档三次,我仍然不确定我是否理解如何使用Ember。选择正确的方法。我认为这是一个非常简单的案例:

我在模型对象(goal_type)中有一个字符串值,我希望双向绑定到一个select,并将该选择默认设置为goal_type字段的起始值。几个问题:

  1. 我无法让select接受模型的初始goal_type。
  2. 所有这些似乎过于复杂 - 必须有一种更简单的方法来设置观察者来修改原始控制器的状态。
  3. 帮助 - 我疯狂地试图解决这个问题。

    Azul.goalController = Ember.Object.create({
    
         loadGoal: function(data) {
           this.set('content', Azul.store.loadAll(Azul.Goal, data));
         },
    
         newGoal: function() {
           goal = Azul.store.createRecord(Azul.Goal, {goal_type: "type2"});
           this.set('content', goal);
           Azul.selectedGoalTypeController.selectGoalType(goal.get('goal_type'));
         }
    
        });
    
        Azul.selectedGoalTypeController = Ember.Object.create({
          selectedGoalType: null,
    
          selectedGoalTypeChanged: function() {
             Azul.goalController.content.set('goal_type', this.selectedGoalType.value);
          }.observes('selectedGoalType'),
    
          selectGoalType: function(goal_type) {
             Azul.goalTypesController.content.forEach(function(item, index, self) {
               if (goal_type == item.value) {
                   self.set('selectedGoalType', item);
               }
             });
          }
        });
    
        Azul.goalTypesController = Ember.ArrayController.create({
            content: [
                {value: 'type1', label: 'Type 1'},
                {value: 'type2', label: 'Type 2'}
            ]
        });
    

1 个答案:

答案 0 :(得分:1)

您必须将值设置为绑定到select选项的对象,而不是值。 这里:

Azul.selectedGoalTypeController.selectGoalType(goal.get('goal_type'));

只是做:

Azul.selectedGoalTypeController.set('selectedGoalType', goal);

顺便说一下,你的问题似乎有点混乱。对不起,如果我误解了你。 我做了一个如何使用Select的简单示例,这里是小提琴:http://jsfiddle.net/Qpkz5/300/

相关问题