2016-04-30 15 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類的代碼,該類允許自動保存關聯。

請注意,只有當您只想保存兩個模型之間的關聯時,並且如果您還修改了其中一個涉及的實體,則關聯將在保存批處理中發送。