2012-03-24 76 views
0

我有這樣的json數據綁定JSON數據到店ubsing ExtJS的

{"GetStudentDetails": 
    {"TotalCount":5, 
    "RootResults":[ 
    {"city":"West Chester","country":"USA","state":"PA ","student_id":100}, 
    {"city":"Philly","country":"USA","state":"PA","student_id":101}, 
    {"city":"Buffalo","country":"USA","state":"NY","student_id":102}, 
    {"city":"Naigra City","country":"USA","state":"NY","student_id":103}, 
    {"city":"West Chester","country":"USA","state":"PA","student_id":104}] 
    } 
} 

如何獲取數據到店嗎? 我正在嘗試使用這樣的模型。

Ext.define('User', { 
       extend: 'Ext.data.Model', 
       fields: [ 
        { type: 'string', name: 'TotalCount' } 
       ], 
       hasMany: [{ model: 'RootResults', name: 'RootResult'}] 
      }); 

      Ext.define("RootResults", { 
       extend: 'Ext.data.Model', 
       fields: [ 
        { type: 'string', name: 'city' }, 
        { type: 'string', name: 'country' }, 
        { type: 'string', name: 'state' }, 
        { type: 'string', name: 'student_id' } 
       ], 
       belongsTo: 'User' 
      }); 

      var store = Ext.create('Ext.data.Store', { 
       model: 'User', 
       proxy: { 
        type: 'ajax', 
        url: 'users.json', 
        reader: { 
         type: 'json' 
        } 
       } 
      }); 

我的模型應該如何?當我給一些更簡單的JSON時,我正在加載商店。我認爲問題是與映射?

回答

1

定義模型

Ext.define("RootResults", { 
       extend: 'Ext.data.Model', 
       fields: [ 
        { type: 'string', name: 'city' }, 
        { type: 'string', name: 'country' }, 
        { type: 'string', name: 'state' }, 
        { type: 'string', name: 'student_id' } 
       ], 
      }); 

與讀者定義中添加兩個參數:

root: 'GetStudentDetails.RootResults' 
totalProperty: 'GetStudentDetails.TotalCount' 

類似的東西...主要思想 - 不要試圖把內部JSON結構,以您的模型 - 正確解析它應該是讀者的責任。