灰烬数据-如果离开表单,则会回滚 [英] Ember Data - rollback if navigating away from a form

查看:36
本文介绍了灰烬数据-如果离开表单,则会回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序具有用于从后端读取的一组实体的新的/编辑表单。

My application has new / edit forms for a set of entities read from a backend.

当我打开这样的表单并填写/编辑一些字段时,然后浏览,即使我没有提交这些更改,记录也会在实体列表中显示为已更改。重新加载应用程序(从后端重新加载数据)解决了该问题,但不是一种选择。

When I open such a form, and fill out / edit some fields, then navigate away, the records appear changed in the entity lists, even though I did not commit those changes. Reloading the app (which reloads the data from the backend) fixes the issue, but is not an option.

我尝试过在表单视图的willDestroyElement中进行一些事务回滚,但这似乎根本上是错误的,因为即使在成功提交表单后它也会被调用(并且实际上在崩溃,试图在状态rootState.loaded.updated.inFlight 下处理X上的事件回滚)。

I've tried doing some transaction rollbacks in the form view's willDestroyElement, but this seems fundamentally wrong since it gets called even after successful form submits (and actually crashes with Attempted to handle event rollback on X while in state rootState.loaded.updated.inFlight).

对于涉及导航的所有用例,我将如何忽略所有未提交的表单更改(类似于按取消按钮,这将执行事务回滚)

How would I go about ignoring all unsubmitted form changes (similar to pressing the Cancel button, which performs a transaction rollback), for any use case that involves navigating away from the forms?

使用Ember rc5,Ember Data 0.13。

Using Ember rc5, Ember Data 0.13.

推荐答案

退出表单路径时,请检查记录的状态。如果它的(isNew OR isDirty)和它的NOT isSaving,则回滚:

When exiting the form route, check the state of the record. If its (isNew OR isDirty) and its NOT isSaving, rollback:

App.FormRoute = Ember.Route.extend({
  deactivate: function() {
    var model = this.controllerFor('form');
    if ( (model.get('isNew') || model.get('isDirty')) && (!model.get('isSaving')) ) {
      model.rollback();
    }
  }
});

这篇关于灰烬数据-如果离开表单,则会回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆