2012-03-20 30 views
0

ASP.NET的安全功能之一在這裏被證明是一座山 - 在返回JSON響應時,「d」屬性添加看起來令人困惑ExtJS when我試圖動態地重新配置一個網格面板,導致它在嘗試生成新的列結構時失敗。ExtJS4 - 在ASP.NET中重新配置網格 - JSON結構問題

I,接着nicholasnet此解決方案: http://www.sencha.com/forum/showthread.php?179861-Dynamic-grid-columns-store-fields

,它工作精美,直到JSON有效載荷被圍繞「d」屬性,例如包裹

{"d":{ 
    "metaData": { 
     "root": "data", 
     "fields": [{ 
      "type": "int", 
      "name": "id", 
      "hidden": true, 
      "header": "id", 
      "groupable": false, 
      "dataIndex": "id" 
     }, ...omitted for brevity...] 
    }, 
    "success": true, 
    "data": [{ 
     "id": "1", 
     "controller": "Permissions", 
     "description": "Allow to see permission by roles", 
     "administrator": true, 
     "marketing": false 
    }] 
    } 
} 

我無法弄清楚如何告訴ExtJS繞過這個問題。 我試過將AJAX閱讀器的「root」屬性設置爲"d.data",但這會導致網格顯示正確的行數但根本沒有數據。

我已經擁有JSON中列元數據("name", "header", "dataIndex")所需的所有屬性描述符,所以我不相信JSON結構是原因。我當時的主要領導是在事件處理程序:

store.on 
({ 
    'load' : 
    { 
     fn: function(store, records, success, operation, eOpts) 
     { 
      grid.reconfigure(store,store.proxy.reader.fields); 
     }, 
     scope: this 
    } 
}, this); 

fieldshistoryStore.proxy.reader.fields部分是不確定的,當我通過「d」 -wrapped JSON。任何人有任何想法,爲什麼這是或如何解決這個問題?

編輯:我店/代理

Ext.define('pr.store.Store-History', { 
    extend: 'Ext.data.Store', 
    model: 'pr.model.Model-History', 
    proxy: { 

     type: 'ajax', 
     url: '/data/history.json', 
     reader: { 
      type: 'json', 
      root: 'd' 
     } 
} 
}); 
+0

您對閱讀器對象的配置是什麼? – sha 2012-03-20 15:00:48

+0

我已將我的商店類添加到OP。 – MHTri 2012-03-20 15:21:27

+0

你可以嘗試閱讀器作爲'讀者:{ 類型:'json', root:'d.data',successProperty:'d.success' }' – 2012-03-20 15:29:31

回答

1
Ext.define('pr.store.Store-History', { 
    extend: 'Ext.data.Store', 
    model: 'pr.model.Model-History', 
    proxy: { 

     type: 'ajax', 
     url: '/data/history.json', 
     reader: { 
      type: 'json', 
      root: 'data', 
      readRecords: function(data) { 
       //this has to be before the call to super because we use the meta data in the superclass readRecords 
       var rootNode = this.getRoot(data); 
       if (rootNode.metaData) { 
        this.onMetaChange(rootNode.metaData); // data used to update fields 
       } 

       /** 
       * @deprecated will be removed in Ext JS 5.0. This is just a copy of this.rawData - use that instead 
       * @property {Object} jsonData 
       */ 
       this.jsonData = rootNode; 
       return this.callParent([rootNode]); // data used to get root element and then get data from it 
      }, 
     } 
    } 
}); 

更新: 你是不是在讀者越來越領域,因爲從獲得的數據不處理您的包裹數據字段的默認代碼,所以您需要更改'readRecords'功能來處理您的自定義數據

+0

我以前試過這個。請再次閱讀OP。它產生的所有行數都是正確的,但沒有顯示數據。 – MHTri 2012-03-20 17:16:09

+0

檢查更新的答案:) – 2012-03-20 17:47:24

+0

謝謝,這是我的同事在調試會議後得出的結論。而不是data.d.data,雖然我使用this.getRoot(data).metaData,以便我可以指定什麼數據的根可以使其他框架決定與ASP.NET一樣安全;) – MHTri 2012-03-21 09:45:18