0

How can you disregard a record that you have changed in an EmberJS view with Ember Data? Something like delete without actually deleting it from the persistent storage.

I thought App.store.removeFromRecordArrays(record); would work.

mabounassif
  • 2,311
  • 6
  • 29
  • 46
  • I think you can use a solution with `commit` similar to [this question](http://stackoverflow.com/questions/9992652/create-temporarty-non-persistent-object-in-ember-data) – MilkyWayJoe Jul 05 '12 at 15:35
  • The thing is that I first find the record with: `record = App.store.findQuery(recordClass, query)`, and then I perform the modifications on the `record`, and sometimes after I've changed the properties I want to disregard the changes. – mabounassif Jul 05 '12 at 15:46
  • I can't use transaction to find something in the persistent layer, it's as if they create a whole new store. – mabounassif Jul 05 '12 at 15:47
  • Can you `Em.copy` that record so you don't touch your collection? then, when you're done editing you can merge it back to the record in your collection or discard it. I do something similar to this in my edit scenario (although I'm still not using ember-data atm) – MilkyWayJoe Jul 05 '12 at 15:50

2 Answers2

0

1) You can use a transaction and rolback the transaction.

2) Or you can just rollback a record by using it's statemanager.

if(record.isDirty)
  record.get('transaction').rollback();

you can for instance loop all records in the stores recordCache and rollback al the dirty records.

I personally use a record rollback mechanism on the willDestroyElement event in a view, so if a user leaves the view, he will be asked to save dirty records.

PatientTransport.FirmView = Ember.View.extend({
  templateName: 'firm-view',

  willDestroyElement: function() {
    if (this.getPath('controller.content.isDirty')) {
      var self = this;
      Bootstrap.ConfirmBox.popup({
        heading: "Some data has changed.",
        message: "Do you want to save changes?",
        callback: function(options, event) {
          if (options.primary) {
            self.getPath('controller.content.transaction').commit();
          } else {
            self.getPath('controller.content.transaction').rollback();
          }             
        }
      });   
    }     
  }
});
pjlammertyn
  • 989
  • 6
  • 10
0

You could extend DS.Model with a flag if this case (deleted client-side but do not delete server-side) is present for this model. Additionally a method is convenient to set this status (call model.deleteLocal()).

DS.Model.reopen({
    deleteLocalFlag: false,
    deleteLocal: function () {
        this.set('deleteLocalFlag',true); 
        this.deleteRecord();
    }
});

Then you need to customize the deleteRecords method in your adapter.

DS.YourAdapter.reopen({
      deleteRecord: function(store, type, model) {
          if (!model.get('deleteLocalFlag') {
              // code for deleting in persitence layer
          } 
          store.didDeleteRecord(model, model.toJSON({associations: true}));
      }
});

Warning: This code was not tested, but works in my head ;)

May be a cleaner solution would be to use the stateManager of the object and transition into a different state instead of setting the flag. But I find the code around stateManager quite difficult to understand und probably is not worth the hassle.

manuel_mitasch
  • 473
  • 3
  • 9