PDA

View Full Version : Unable to work with nested data usign association



webfriend13
1 Mar 2012, 4:55 AM
Please have a look at the model structure. I have posted the question below. Kindly provide source code for entire implementation as i am very new to ExtJS.

Ext.define('Test.model.CustomerModel', {
extend: 'Ext.data.Model',
requires: ['Test.model.OrderModel'],
idProperty: 'id',
fields: ['id', 'CustomerName'],
hasMany: { model: 'Test.model.OrderModel', name: 'ordermodels' }
});
//==================
Ext.define('Test.model.OrderModel', {
extend: 'Ext.data.Model',
idProperty: 'id',
fields: ['id', 'OrderName','customermodel_id'], // customermodel_id should get value from customerModel's id
belongsTo: 'Test.model.CustomerModel'//,
});

//============

Ext.define('Test.store.CustomerStore', {
extend: 'Ext.data.Store',
requires: ['Test.model.CustomerModel'],
model: 'Test.model.CustomerModel',
proxy: {
type: 'ajax',
url: 'Scripts/extjs-app/data/Customer.json',
reader: {
type: 'json',
root: 'Customers'
}
},
autoLoad: true,
});

//===========================
Customer.JSON file:
{
Customers: [
{
id: "AAAAA",
CustomerName: "AAAAA",
ordermodels: [
{
id : "Order 1",
OrderName : "Order 1",
customermodel_id : "AAAAA"
},
{
id : "Order 2",
OrderName : "Order 2",
customermodel_id : "AAAAA"
},
]
},
{
id: "BBBBBB",
CustomerName : "BBBBBB",
ordermodels: [
{
id : "Order 3",
OrderName : "Order 3",
customermodel_id : "BBBBBB"
},
{
id : "Order 4",
OrderName : "Order 4",
customermodel_id : "BBBBBB"
}
]
},
{
id: "CCCCC",
CustomerName : "CCCCC",
ordermodels: [
{
id : "Order 5",
OrderName : "Order 5",
customermodel_id : "CCCCC"
},
{
id : "Order 6",
OrderName : "Order 6",
customermodel_id : "CCCCC"
}
]
},
{
id: "DDDDD",
CustomerName: "DDDDD",
ordermodels: [
{
id : "Order 7",
OrderName : "Order 7",
customermodel_id : "DDDDD"
},
{
id : "Order 8",
OrderName : "Order 8",
customermodel_id : "DDDDD"
}
]
}
]
}


What I want to acheive:
i have two grid, one showing all the customers and one where when I select any customer, it shows the order placed by that customer.
What I have:
2 Models, 1 Store, 1 Controller & view, A json file which contains the data.
Problem:
I am not able to get details of child elements (Order in this case) through the store.
I am not sure this is the right approach.
I am not sure the JSON file is in correct format.
Please feel free to modify the files and code.
All I want is to have single store which will load data through the JSON file and it should first populate customer grid showin all customer and when
the user selects a record in customer grid, it should populate orders grid showing record specifc to customer. is it possibelt to do this while maintaining
association between the models?
Feel free to suggest changes. I am very new to ExtJs.