Ember.js 自动提交对具有余烬数据的其他记录的附带更改
我有一个应用程序,其中一个用户有一个团队 我在你创建新团队的表格上。当我创建一个团队时,我的api返回新团队的json,还返回其所有者用户的一些json,因为创建一个团队会更改用户中的标志Ember.js 自动提交对具有余烬数据的其他记录的附带更改,ember.js,commit,ember-data,sideloading,Ember.js,Commit,Ember Data,Sideloading,我有一个应用程序,其中一个用户有一个团队 我在你创建新团队的表格上。当我创建一个团队时,我的api返回新团队的json,还返回其所有者用户的一些json,因为创建一个团队会更改用户中的标志 { "team": { "id":139, "name":"myteam", "manager_nickname":"Miguel", "stadium_name":"riazr", "user_id":10, "next_match_id":null,
{
"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;
}
});
}
});
正如我在评论中所说,有一种方法可以自动提交其他记录中的侧载更改?自动响应:
这是我的错误。问题不在于ember没有提交从服务器接收的数据。此数据不会将记录标记为脏记录
transitionAfterSave: function(){
if (this.get('model.id')){
var team = this.get('model');
App.set('currentUser.team', team);
// Was this line the one that marked the record as dirty.
// I realized that the current user was not automatically caching the team
// identity map mechanism as the own. I didn't gave importance to this and
// just stablished the relation manually. BAD IDEA
// Identity map should recognise automatically the relation.
// The problem was in the json. I added a team_id: XXX key on the side
// loaded data of the user, all worked.
// Is still strange to me to send foreign_keys that don't really exist in the
// database (is team the one that has a user_id field) and I didn't realized
// that ember is a different world.
//
// Now this line has gone and all works.
App.set('currentTeam', team);
this.transitionToRoute('dashboard');
}
}.observes('model.id')