【发布时间】:2015-06-03 18:46:10
【问题描述】:
对于以下应用程序流程是否有推荐的方法或模式?
- 用户输入路线
- 检索路线模型并通过模板呈现给用户。
- 模板输入绑定到模型属性,因此当用户修改它们时,存储中的数据会即时更新。
- 用户在没有点击“保存”的情况下离开路线
我看到的问题是模型保留了用户更改的值,尽管它们没有被持久化到后端。
我目前解决这个问题的方法是这样的,感觉很笨拙:
在 setupController 钩子中,我使用 Ember.copy 从模型中设置了几个默认属性值来中断绑定:
setupController: function(controller, model) {
this._super(controller, model);
var goals = model.get('goals');
controller.set('goalOne', Ember.copy(defaultGoal.get('goalOne')));
controller.set('goalTwo', Ember.copy(defaultGoal.get('goalTwo')));
controller.set('goalThree', Ember.copy(defaultGoal.get('goalThree')));
}
将我的输入绑定到这些值:
{{input value=goalOne type='text'}}
单击保存时,调用updateModel() 方法将这些值设置回模型并保留到后端。
updateModel: function() {
var model = this.get('content');
model.set('goalOne', this.get('goalOne');
model.set('goalTwo', this.get('goalTwo');
model.set('goalThree', this.get('goalThree');
}
actions: {
save: function() {
this.updateModel();
this.get('content').save();
}
}
【问题讨论】:
标签: ember.js ember-data