使用ember-data自动提交对其他记录的侧载更改

时间:2013-06-15 00:46:39

标签: 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,
    "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;
      }
    });
  }

});

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

1 个答案:

答案 0 :(得分:0)

自动回复:

这是我的错。问题不在于没有提交从服务器收到的数据。此数据不会将记录标记为脏。

  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')