Skip to content

Instantly share code, notes, and snippets.

@lukemelia
Created August 13, 2012 20:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukemelia/3343996 to your computer and use it in GitHub Desktop.
Save lukemelia/3343996 to your computer and use it in GitHub Desktop.
Batch transaction save
App.profileEditorController = Em.ObjectController.extend({
addressBinding: 'content.address'
});
App.Router = Em.Router.extend({
root: Em.Route.extend({
profile: Em.Route.extend({
route: 'profile',
edit: Em.Route.extend({
route: 'edit',
connectOutlets: function(router) {
router.get('applicationController').connectOutlet('profileEditor', router.get('currentUser'));
},
save: function(router) {
var transaction = router.get('store').transaction(),
user = router.getPath('profileEditorController.content'),
address = user.get('address');
transaction.add(user);
transaction.add(address);
transaction.commit();
transaction.then(function(){
router.transitionTo('profile')
}, function(){
// Do somphing on error
});
// OR OTHER PROMISE API LIKE:
transaction.done(function(){
router.transitionTo('profile')
});
transaction.fail(function(){
// Do somphing on error
});
}
})
})
})
});
@boy-jer
Copy link

boy-jer commented Apr 19, 2013

Hi Luke,

I was wondering how we could add multiple association in the save function or elsewhere using router version 2. Just to continue with your code sample since the idea is thesame of adding the associated models in the save evet in the router's events hook :

 save: function(author) {

     user = router.getPath('profileEditorController.content'),
      post = this.controllerFor('post.content');
      comment = post.get('comments');

      transaction.add(post);
       transaction.add(comment);
       transaction.commit();
 }

This also doe not work:

 save: function(author){
     author.one('didCreate', this, function(){
        this.transitionTo('post.index'); 
    }); 

    var post = this.controllerFor('post').get('model');
     var comment = post.get('comments');

    this.get('currentModel.transaction').add(post);
     this.get('currentModel.transaction').add(comment);

       author.get('transaction').commit();
   }

Though it will save, the association are not added in the created record. What do I need to change. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment