我已经阅读了所有测试两次,文档阅读了三遍,但我仍然不确定我是否了解如何使用 Ember。选择正确的方式。我有一个我认为非常简单的案例:
我在模型对象(goal_type)中有一个字符串值,我想双向绑定到一个选择,并将该选择默认为目标类型字段的起始值。几个问题:
- 我无法让选择接受模型的初始目标类型。
- 所有这些似乎都太复杂了——必须有一种比设置观察者来修改原始控制器的状态更简单的方法。
帮助-我想弄清楚这一点我快疯了。
Azul.goalController = Ember.Object.create({
加载目标:函数(数据){
this.set('content', Azul.store.loadAll(Azul.Goal, data));
},
新目标:函数(){
目标 = Azul.store.createRecord(Azul.Goal, {goal_type: "type2"});
this.set('内容', 目标);
Azul.selectedGoalTypeController.selectGoalType(goal.get('goal_type'));
}
});
Azul.selectedGoalTypeController = Ember.Object.create({
选定目标类型:空,
selectedGoalTypeChanged:函数(){
Azul.goalController.content.set('goal_type', this.selectedGoalType.value);
}.observes('selectedGoalType'),
选择目标类型:函数(目标类型){
Azul.goalTypesController.content.forEach(function(item, index, self) {
if (goal_type == item.value) {
self.set('selectedGoalType', item);
}
});
}
});
Azul.goalTypesController = Ember.ArrayController.create({
内容: [
{值:'type1',标签:'Type 1'},
{值:'type2',标签:'Type 2'}
]
});