2011-04-21 110 views
1

我正在與Ext JS & Rails作爲後端..我有myysql數據庫有父子關係,即一對多的關係......我想要一個「單一」JSON存儲可以用來加載「GRID」數據與家長&子記錄。 也有任何方式綁定兩個 - 表單以及網格到同一商店,但有不同的jsonreaders?即表單的數據讀取器將讀取字符串root:'customers'並且網格將讀取字符串root:'products'? 的JSON看起來是這樣的:extjs nested json store rails

{ 
     "customers": [{ 
      "amt": 8000, 
      "custnm": "rashmi", 
      "id": 2, 
      "purdt": "2011-04-27", 
      "products": [{ 
       "amt": 40, 
       "customer_id": 2, 
       "id": 3, 
       "prodnm": "oil", 
       "qty": 1, 
       "rate": 40 
      }] 
     }, { 
      "amt": 300000, 
      "custnm": "bhumika", 
      "id": 3, 
      "purdt": "2011-04-14", 
      "products": [{ 
       "amt": 40, 
       "customer_id": 3, 
       "id": 1, 
       "prodnm": "soap", 
       "qty": 20000, 
       "rate": 20 
      }, { 
       "amt": 150, 
       "customer_id": 3, 
       "id": 2, 
       "prodnm": "shampoo", 
       "qty": 3000, 
       "rate": 50 
      }, { 
       "amt": null, 
       "customer_id": 3, 
       "id": 14, 
       "prodnm": "aa", 
       "qty": 2, 
       "rate": null 
      }] 
     }, { 
      "amt": 15000, 
      "custnm": "Shruti", 
      "id": 13, 
      "purdt": "2011-04-08", 
      "products": [] 
     }, { 
      "amt": 200000, 
      "custnm": "Jayesh", 
      "id": 14, 
      "purdt": "2011-03-31", 
      "products": [] 
     }, { 
      "amt": 220000, 
      "custnm": "SHRUTI", 
      "id": 15, 
      "purdt": "2011-04-06", 
      "products": [] 
     }, { 
      "amt": 10000, 
      "custnm": "RASHMI", 
      "id": 24, 
      "purdt": "2011-04-06", 
      "products": [] 
     }], 
     "results": 6 
    } 
+0

如果您嘗試顯示json,請使其可讀。這裏試試這個:http://jsbeautifier.org/。在你的json中,你忘了添加最後一個「}」 – 2011-04-21 07:10:33

回答

2

新分機JS 4具有模型關聯,這將解決調用user.orders()返回與訂單模型構成的存儲問題(http://docs.sencha.com/ext-js/4-0/guide/data

// each User hasMany Orders 
Ext.define('User', { 
    extend: 'Ext.data.Model', 
    fields: ['id', 'name', 'email'], 
    proxy : { 
     type: 'rest', 
     url : '/users', 
     reader: 'json' 
    }, 

    hasMany: 'Orders' 
}); 

// each Order belongsTo a User, and hasMany OrderItems 
Ext.define('Order', { 
    extend: 'Ext.data.Model', 
    fields: ['id', 'user_id', 'status'], 
    belongsTo: 'User', 
    hasMany: 'OrderItems' 
}); 

// each OrderItem belongsTo an Order 
Ext.define('OrderItem', { 
    extend: 'Ext.data.Model', 
    fields: ['id', 'order_id', 'name', 'description', 'price', 'quantity'], 
    belongsTo: 'Order' 
}); 

,因爲用戶模型中所定義的締合hasMany:'訂單'。

User.load(123, { 
    success: function(user) { 
     //we can iterate over the orders easily using the Associations API 
     user.orders().each(function(order) { 
      console.log(order.get('status')); 

      //we can even iterate over each Order's OrderItems: 
      order.orderItems().each(function(orderItem) { 
       console.log(orderItem.get('title')); 
      }); 
     }); 
    } 
}); 
+0

thanx @sdavids .. .im面臨使用extjs 4即時通訊4使用extjs 3即時通訊...我的UI組件已使用extjs設計器1.1.2開發...我使用extjs 4的js文件,我得到了很多錯誤....所以你可以建議我任何選擇? – Shruti 2011-04-28 05:23:11

0

您可以使用覆蓋默認的渲染方法。定義列的渲染器方法。

renderer: function(value, metaData, record, rowIndex, colIndex, store) { 
     // you can use record.get method to access the data you require 
     return the_retrived_data; 
} 

您需要使用mapping屬性爲您的JSON商店的字段映射到嵌套的JSON。例如,如果你需要訪問產品的名稱,你可以有:

{name: 'productname', mapping: 'products.prodnm'} 

您也可以使用數組符號來代替點運算符的。例如:"products['prodnm']"

+0

thanx @abdel ...「mapping」在1個「客戶」記錄有多個「產品」記錄時不起作用..即不適用於數組.. :( – Shruti 2011-04-21 07:23:25

+0

嘗試使用渲染器的方法,並訪問記錄對象或商店對象,以獲得正確的值來顯示... – 2011-04-21 07:33:30

+0

嘿你可以plzzz給我一個例如您提供的rendere函數.. – Shruti 2011-04-21 08:34:39