2016-04-30 17 views
0

标题说明了所有内容,以下是详细信息。为什么session.getSaveBatch()在添加子记录时未定义 - 分机5.1.1

我有两个相关的型号,用户&角色。

用户定义为角色:

Ext.define('App.model.security.User', { 
    extend: 'App.model.Base', 

    entityName: 'User', 

    fields: [ 
     { name: 'id' }, 
     { name: 'email'}, 
     { name: 'name'}, 
     { name: 'enabled', type: 'bool'} 
    ], 

    manyToMany: 'Role' 
}); 

然后,我有用户的网格和表单编辑用户的数据,包括他的角色。

事情是,当我尝试添加或删除用户的角色时,以后调用session.getSaveBatch()返回undefined,然后我无法启动批处理将修改发送到服务器。

我该如何解决这个问题?

回答

0

嗯,这很有意思,我学到了很多有关解决这个简单的问题EXT。

我碰到的解决方案是重写BatchVisitor类利用该事件的事件处理程序从Session类的私有方法visitData onCleanRecord提高。

因此,对于每个记录,我在矩阵中查找左侧实体,如果有变化,则调用在BatchVisitor原始类上定义的onDirtyRecord处理函数。

代码:

Ext.define('Ext.overrides.data.session.BatchVisitor', { 
    override: 'Ext.data.session.BatchVisitor', 

    onCleanRecord: function (record) { 
     var matrices = record.session.matrices 
      bucket = null, 
      ops = [], 
      recordId = record.id, 
      className = record.$className; 

     // Before anything I check that the record does not exists in the bucket 
     // If it exists then any change on matrices will be considered (so leave) 
     try { 
      bucket = this.map[record.$className]; 
      ops.concat(bucket.create || [], bucket.destroy || [], bucket.update || []); 

      var found = ops.findIndex(function (element, index, array) { 
       if (element.id === recordId) { 
        return true; 
       } 
      }); 

      if (found != -1) { 
       return; 
      } 

     } 
     catch (e) { 
      // Do nothing 
     } 

     // Now I look for changes on matrices 
     for (name in matrices) { 
      matrix = matrices[name].left; 

      if (className === matrix.role.cls.$className) { 
       slices = matrix.slices; 

       for (id in slices) { 
        slice = slices[id]; 
        members = slice.members; 

        for (id2 in members) { 
         id1 = members[id2][0]; // This is left side id, right side is index 1 
         state = members[id2][2]; 

         if (id1 !== recordId) { // Not left side => leave 
          break; 
         } 

         if (state) { // Association changed 
          this.onDirtyRecord(record); 
          // Same case as above now it exists in the bucket (so leave) 
          return; 
         } 
        } 
       } 
      } 
     } 
    } 
}); 

这都非常好,我的需求,很可能它不会成为别人的最好的解决办法,但可以是一个起点反正。

最后,如果还不清楚,这是否让getSaveBatch方法能够检测关系变化。

0

在阅读了很多内容后,我发现Ext不会保存两个模型之间至少在5.1.1之间的变化关系。 我必须通过在默认值为false的左侧模型(我命名为isDirty)上放置一个aditional字段来解决此问题,并将其设置为true以强制会话使用getSaveBatch将更新发送到服务器。

后来我将深入研究将代码覆盖写入BatchVisitor或自定义BatchVisitor类的代码,该类允许自动保存关联。

请注意,只有当您只想保存两个模型之间的关联时,并且如果您还修改了其中一个涉及的实体,则关联将在保存批处理中发送。