我正在尝试在提交时保存表单,但在任何其他情况下都放弃更改。问题是即使在提交之后 isDirty 标志也是真的。
App.UserController = Ember.Controller.extend({
enterEditing: function() {
this.transaction = App.store.transaction();
this.transaction.add(this.get('content'));
},
updateEditing: function() {
console.log('update saved');
this.transaction.commit();
this.transaction = null;
}
});
App.UserView = Ember.View.extend({
templateName: 'edit-user',
willDestroyElement: function() {
console.log(this.getPath('controller.content.isDirty'));
if (this.getPath('controller.content.isDirty')) {
console.log('unsaved changes');
this.getPath('controller.content.transaction').rollback();
}
}
});
还有我的路由器部分:
showNew: Ember.Route.extend({
route: '/user/new',
cancelEditUser: Ember.Route.transitionTo('index'),
connectOutlets: function(router) {
router.get('applicationController').connectOutlet('user');
router.get('userController').enterEditing();
}
}),
update: function(router, event) {
router.get('userController').updateEditing();
router.transitionTo('index');
}