我有一个用户有一个团队的应用程序。
我在您创建新团队的表格上。当我创建团队时,我的 api 返回新团队的 json 并返回其所有者用户的一些 json,因为创建团队会更改用户中的标志。
{
"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
}
]
}
我的应用程序在控制器初始化时创建事务并调用 createRecord 来填充控制器的模型。该模型的属性自动填充到表单字段的绑定。提交操作验证客户端中的某些字段,如果一切看起来都正确,则提交事务。
这里的代码:
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;
}
});
}
});
正如我在评论中所说,有一种方法可以自动提交其他记录中的侧载更改?