Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup a callback after a transaction has committed

I'm trying to achieve something that I guess has a simple answer but somehow I can't find it reading the source code of ember-data.

I would like to trigger an action after a particular set of modifications have been committed to the datastore. This my current code:

var transaction = App.store.transaction();
user = App.get('currentUser');
transaction.add(user);
user.set('name', "Michael Jackson");
transaction.commit();
App.navigate('dashboard');

My first attempt was to add an observer over this user's isDirty property, but a) it doesn't seem idiomatic and more importantly b) I would have to remove manually that observer at the end - that's prone to errors if some exception raises in between and doesn't get removed, for instance.

Any ideas? How would you do this?

like image 606
josepjaume Avatar asked May 13 '12 11:05

josepjaume


1 Answers

You can use didCreate and didUpdate on the model. I'm assuming you have a user model so something like:

MyApp.User = DS.Model.extend({
  first: DS.attr('string'),
  page: DS.attr('string'),
  didUpdate: function() {
    //Do something
  },
  didCreate: function() {
    //do something
  }
});
like image 102
Cory Loken Avatar answered Dec 19 '22 18:12

Cory Loken