下面的应用程序流是否有推荐的方法或模式?
我看到的问题是,模型保留了用户更改的值,尽管它们没有被保存到后端。
我目前处理这个问题的方法是这样的,而且感觉很笨重:
在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();
}
}
发布于 2015-06-03 10:58:11
看看ember数据-路线。它允许您直接绑定到模型,并自动回滚,如果用户导航而不保存。
https://stackoverflow.com/questions/30628206
复制相似问题