0

I have a modal window with forms/select inside and when i click on some select inputs, some forms appears, some disappears. Everything works fine, but the only problem for me is the 'cancel' button.

Indeed, i would love to rollback changes made on the models. i am fully aware of the rollback method, but the forms manipulations involve more than properties modifications, it also involves deletion of models.

Therefore, the global idea would be to make a copy of my model everytime i am entering the modal window. Then, once the user 'commit' theses changes, i merge the data to my existing models.

Do you know a more elegant solution ?

Thanks you very much !

Muenodi
  • 19
  • 2

1 Answers1

0

It sounds like you're using Ember Data and that you're aware of using modelOfForm.rollback();.

If your problem is that you are doing deletes of related models, that run behind your form fields (e.g. belongsToModelOfFormField.deleteRecord();). Then it might be more elegant and less coding to manually do the Rollbacks on your model and the related models from within your Route's

{ actions: {
   cancelForm : function(e) {
      ...
      belongsToModelOfFormField1.rollback(); 
      belongsToModelOfFormField2.rollback(); 
      // etc.
      modelOfForm.rollback();`
   }
}

(Assuming your handlebar button is something like <button type="button" {{action cancelForm}} >cancel</button>.)

A duplicate cache of all your form model for safe keeping and doing your own resorting of the info sounds like an extra headache, but as devs we all know sometimes you're reduced to that.

Another example would be to have async on the child models, and to do modelOfForm.get('content').rollback(); as Toran explains here, performing rollback on model with hasMany relation . Hope that leads you to your answer and less coding :).

Community
  • 1
  • 1
Eric D. Johnson
  • 10,219
  • 9
  • 39
  • 46
  • Thank you for your answer ! But there is something wrong with your answer : it seems i cannot rollback a deleted record (deleteRecord()). Do you have the same issue ? (https://github.com/emberjs/data/issues/1737) – Muenodi May 26 '14 at 10:18
  • Nope you're right, this is still an open issue. If they allow `deleteRecord()` to work in a rollback like the API claims, then I'll post something back over here about the update. – Eric D. Johnson May 28 '14 at 15:17
  • Found a cool plugin that let's you delete models easily by combining Ember and OrbitJS. This [slide](https://speakerdeck.com/dgeb/autonomous-web-applications-with-ember-dot-js-and-orbit-dot-js?slide=47) starts talking about some baked in methods like Remove that would work with LocalStorage or JSON API and a server. ToDo List [example code](https://github.com/opsb/ember-orbit-todos). – Eric D. Johnson Jun 23 '14 at 12:57