PDA

View Full Version : Synchronize store with associations



Mis63
24 Dec 2010, 12:35 AM
I create a model (Account) having a associated model (Order) :


Ext.regModel('Order', {
fields: [
{name:'id', type:'int'},
{name:'date', type:'date', dateFormat:'c'},
{name:'status', type:'int'},
...
]
});

Ext.regModel('Account', {
fields: [
{name:'id', type:'int'},
{name:'name', type:'string'},
{name:'important', type:'boolean'},
...
],
associations: [
{
type: 'hasMany',
model: 'Order',
name: 'orders'
}
]
});


Then I can load the accounts and associated orders with a unique store :


var store = new Ext.data.Store({
model: 'Account',
proxy: {
type: 'ajax',
url: 'AccountProxy',
reader: {
type: 'json',
root: 'accounts',
idProperty: 'id'
},
writer: {
type: 'json',
root: 'interventions'
}
}
});


The Java servlet responding to AccountProxy url sends JSON response formatted as below :


{
"accounts": [
{
"id":63,
"name":"Toto",
"important":true,
"orders":[
{"id":325,"date":"2010-10-06T12:03:00", status:2},
{"id":403,"date":"2010-12-13T16:51:00",status:1}
]
},
{
"id":94,
"name":"Titi",
"important":false,
"orders":[
{"id":128,"date":"2010-04-01T08:49:00",status:2}
]
}
]
}


It works fine and allow to load simultaneously the accounts, and for each account its orders. Very good. ;)

Load of data is ok, but I have a problem to save them later.
Imagine that the application changed the status of the second order of the first account, and the important property of this account :


var account = store.getAt(0),
order = account.orders().getAt(1);
account.set('important', false);
order.set('status', 2);

and synchronize the store to the Ajax proxy :


store.sync();


The problem is that the associated items (orders) are not sent to the Ajax proxy; it received only the modified account :


{
"accounts": [
{
"id":63,
"name":"Toto",
"important":true
}
]
}

Very problematical. How to save the modified orders (there can be many accounts and orders modified). :-?
May be un parameter should be defined somewhere to indicate if sync method of store should write the associated records that are also modified.

mbhockey22
29 Jun 2011, 2:17 AM
any ideas? how did you solve it?