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.
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.
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();
}
}
});
}
}
});
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.