Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to revert unsaved changes #101

Closed
terrysahaidak opened this issue May 28, 2019 · 3 comments
Closed

Ability to revert unsaved changes #101

terrysahaidak opened this issue May 28, 2019 · 3 comments

Comments

@terrysahaidak
Copy link

terrysahaidak commented May 28, 2019

I was looking through the docs and didn't find a way to revert changes to the model.

I have a profile settings screen, which has some base editable info of a user and a "save" button.
Sometimes user can decide to dismiss editing. But any changes to the model are already in my model.

How can I decline changes or make a change to the real node just after the save have happened?

@faassen
Copy link
Contributor

faassen commented May 28, 2019

Thanks for your question!

mstform doesn't have direct support for this (at the moment, we could potentially add support). It's rather fundamental to its architecture that the node is kept in sync with the form itself. But you can make it work by keeping a MST snapshot of your node of the state before the form was shown, and then if the user presses cancel, apply that snapshot again.

We could add support for this - make such a snapshot when form state is made, and apply it automatically to the node if the user calls a 'revert' method on the state.

@terrysahaidak
Copy link
Author

terrysahaidak commented May 29, 2019

Hi @faassen, thanks for your reply. I've realized that the easiest way to reach it will be something like:

const m = M.create();

const initialState = getSnapshot(m);

function save(node) {
  applySnapshot(m, getSnapshot(node));
}

const form = form.state(clone(m), {
  backend: {  
    save,
  }
};

function reset() {
  applySnapshot(form.node, initialState);
}

I think it would be cool to have it in docs as well.

@RickLucassen
Copy link
Contributor

Better late than never ;)

This has been implemented in version 1.32.0, also see: https://github.com/isprojects/mstform#restore-state

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants