2

The following save function commits the whole store. How can I commit just this? this.commit(); doesn't work.

app.js

App.UserController = Ember.ObjectController.extend({
  save: function() {
    this.get('store').commit();
  }  
})
wintermeyer
  • 8,178
  • 8
  • 39
  • 85

1 Answers1

1

You can create a transaction, add a record to it, and then commit (or rollback) the transaction:

startEditing: function() {
  this.transaction = this.get('store').transaction();
  this.transaction.add(this.get('content');
},

save: function() {
  this.transaction.commit();
}
Dan Gebhardt
  • 3,251
  • 1
  • 18
  • 15
  • 2
    in addition to that, setting `this.transaction` to null after the commit might be a good idea. – Finn MacCool Apr 15 '13 at 09:13
  • @FinnMacCool I agree. Here's a controller from an example project I wrote that follows this pattern: https://github.com/dgeb/ember_data_example/blob/master/app/assets/javascripts/controllers/contact_edit_controller.js – Dan Gebhardt Apr 15 '13 at 13:29
  • Sometimes I have problems with this approach, specially with `rollback`. I'm currently using `this.get('store.currentTransaction').commit() // or .rollback()` in the `Route` and I haven't had any problems since. This way I don't have to get a transaction instance from the store or have a method to start a transaction. – MilkyWayJoe Apr 15 '13 at 13:58
  • I'm looking for a solution where I can commit a single object at a time. I tried the above solution but it commits ever dirty object and not just a single one. Any ideas? – wintermeyer Apr 15 '13 at 16:08