1

我有一个用户有一个团队的应用程序。

我在您创建新团队的表格上。当我创建团队时,我的 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;
      }
    });
  }

});

正如我在评论中所说,有一种方法可以自动提交其他记录中的侧载更改?

4

1 回答 1

0

自动回复:

这是我的错误。问题不在于 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')
于 2013-06-15T01:31:41.790 回答