I have a application where a User has one Team.
I am on the form where you create a new team. When I create a team, my api returns the json of the new team and also returns some json of its owner user, because creating a team changes a flag in the user.
{
"team": {
"id":139,
"name":"myteam",
"manager_nickname":"Miguel",
"stadium_name":"riazr",
"user_id":10,
"next_match_id":null,
"kit_id":139
},
"users":[
{
"id":10,
"team_ready":true
}
]
}
My application creates transaction when the controller is initialized and calls createRecord on to fill the controller's model. The attributes of this model are automatically filled with bindings to the form fields. The submit action validates some fields in the client side and if all seems correct, commits the transaction.
Here the code:
App.TeamsNewController = Ember.Controller.extend(Ember.Validations.Mixin,{
// Validations
//
// ..code omitted..
// Bindings
//
// ..code omitted..
// Observers
//
transitionAfterSave: function(){
if (this.get('model.id')){
//
// At this point the team has been successfully saved.
// App.get('currentTeam.isDirty') # => false
// App.get('currentUser.isDirty') # => true
//
// The transaction committed the changes in the team record. Committing those
// changes bring some other changes to the team's user via side-load, and those
// changes are not commited yet.
//
// I can understand it, since the commit was made before these data where
// returned, but I think that it should be a way to autocommit changes from
// sideloaded data.
// If commit a change returns side-loaded data from the server, this data should
// be commited as well. Server's word is sacred, isn't it?
//
var team = this.get('model');
App.set('currentUser.team', team);
App.set('currentTeam', team);
this.transitionToRoute('dashboard');
}
}.observes('model.id'),
// Actions
//
// This function is called in the #setupController method of the route
//
buildTeam: function(){
this.transaction = this.get('store').transaction();
var team = this.transaction.createRecord(
App.Team,
{ managerNickname: App.get('currentUser.firstName') }
);
this.set('model', team);
},
submit: function() {
var controller = this;
this.validate().then(function(){
if (controller.get('isValid')){
controller.transaction.commit();
controller.transaction = null;
}
});
}
});
As I say in the comment, there is a way to have the sideloaded changes in other record autocommited?