Skip to content

Instantly share code, notes, and snippets.

@darthdeus
Last active December 12, 2015 02:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save darthdeus/4701758 to your computer and use it in GitHub Desktop.
Save darthdeus/4701758 to your computer and use it in GitHub Desktop.

Rollback a transaction when user exits a route

I just stubmled upon an issue in my app where a user can go to edit his profile, but instead of saving the form he just leaves to another page where he can post a status (User has many Statuses).

But to make sure that he's just posting a status and not committing something else I want to put that status into a separate transaction

createStatus: function() {
  var transaction = this.get("store").transaction(),
      text = this.get("text"),
      user = App.currentUser;

  if (/^\W+$/.test(text)) {
    return;
  }

  // this won't work if the user was modified on a previous route
  transaction.add(user);
  user.get("statuses").createRecord({ text: text, user: user });
  transaction.commit();

  this.set("text", "");
},

Solution

What I came up with is basically adding the model on every edit action to a transaction in enter and then doing a rollback on exit.

enter: function() {
  var user = this.modelFor("user");
      transaction = user.get("store").transaction();

  transaction.add(user);
},

exit: function() {
  this._super();
  this.modelFor("user").get("transaction").rollback();
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment