Force reload of dirty/invalid model in Ember

依然范特西╮ 提交于 2019-12-14 04:21:28

问题


I am trying to build out edit functionality for a Goal record. On the Goal index page, there is an edit button next to each goal. When clicked, each field becomes editable. Upon clicking Save, the changes are saved to the server. So far so good.

There is also a Cancel button. When a user clicks it, I need to reset the state of the model to what it was before they changed things. goal.rollback() in the controller works fine for this. Except, if the user has already clicked Save but there were server side validation failures. In this case, attempting to rollback() throws Uncaught Error: Attempted to handle event `reloadRecord` on <App.Goal:ember123:1234> while in state root.loaded.updated.invalid.

If instead I try to goal.reloadRecord I get Uncaught Error: Attempted to handle event `reloadRecord` on <App.Goal:ember123:1234> while in state root.loaded.updated.invalid.

Same deal with goal.unloadRecord. I have tried massaging the state like this:

state = goal.get('currentState') #this code makes me sad.
state.isValid = true
state.isError = false

And like this:

goal.transitionTo('loaded.saved')

To no avail. Is make zero sense to me the reloading or unloading a record should be statefull.

Any assistance would be greatly appreciated. Again, I'm trying to take a dirty, invalid record in ember and get it back to a happy state either by rolling back changes, or just reloading it from the server.

EDIT: Ember-data v1.0.0-beta.3-4-g169793e, ember Version: 1.1.2


回答1:


Here's a working example, change the color then hit save.

http://emberjs.jsbin.com/OxIDiVU/44/edit

PR submitted https://github.com/emberjs/data/pull/1590



来源:https://stackoverflow.com/questions/20642136/force-reload-of-dirty-invalid-model-in-ember

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