copenndthagen
copenndthagen

Reputation: 50752

BackboneJS Reset Model

I have a details view from where I load a dialog.

This dialog has a form where user can enter some values (corresponding to my model attributes)

I pass the model from my details view and set values in the same model (on change action of form fields)

Now the issue is the user can change some form fields (which updates my model values) and click on Cancel. However I want the model to go back to it's initial state when the dialog was loaded. (NOT exactly the default state)

How can I handle this ?

Upvotes: 0

Views: 201

Answers (2)

Dmitriy
Dmitriy

Reputation: 340

With model.previousAttributes() you can get attributes only before the last call of set method.

You can try to save state manually before dialog starts up and set it back if user changes his mind

var state = this.model.toJSON();
...start up dialog...

...in case of cancel...
this.model.set(state);

Upvotes: 0

sajeevp
sajeevp

Reputation: 42

This is the best option to get the previous values.

model.set(model.previousAttributes());

Upvotes: 1

Related Questions