Ember Data - rollback if navigating away from a form

匿名 (未验证) 提交于 2019-12-03 01:20:02

问题:

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.

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?

Using Ember rc5, Ember Data 0.13.

回答1:

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();     }   } });


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!