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 Status
es).
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", "");
},
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();
},