View Full Version : how to send associated model on record.save()

28 Jan 2012, 3:13 PM

I have a Extjs model person and model phoneNumber (also respective model on database)
" person has many phoneNumber "

On ExtJs the model person has a association hasMany phoneNumber.

When i create a person record i add the phoneNumbers to that record.
Then i send to save the person record.

Ext.onReady(function() {

Ext.define('Phone', {
extend: 'Ext.data.Model',
fields: [
{name:'phoId' ,type:'int'},
{name:'perId' ,type:'int'},
{name:'phoNumber' ,type:'string'},
idProperty: 'phoId',
{ type: 'direct',
create : phoQuery.insert,
read : phoQuery.select,
update : phoQuery.update,
destroy : phoQuery.delete

Ext.define('Person', {
extend: 'Ext.data.Model',
fields: [
{name:'perId' ,type:'int'},
{name:'perName' ,type:'string'},
associations: [
{ type: 'hasMany', model: 'Phone', name : 'phones', associationKey: 'phone' }
idProperty: 'perId',
{ type: 'direct',

read : perQuery.select,
create : perQuery.insert,
update : perQuery.update,
update : perQuery.delete,


Ext.define('PersonStore', {
extend: 'Ext.data.Store',
model: 'Person',
autoLoad: false

// Logic
var myPerson = Ext.create('Person');


{phoNumber : '12342425'},
{phoNumber : '056-23-3232'}



this save() only sends the person data. How can i send the phone numbers too ? in the same call.

In the server side i have a function (perInsert) to insert a new person to the dabase, and then, save his phones if theres any, using the person Id that the database asigned after the person insert. this is working.

The problem is to send person + his phones to that server function.

other ways im trying
Send 2 saves (save and sync)


cant do this because those phone numbers dont have the person id (perId) set yet so the server wont know to which person they belongs to. I cant set phones perId before the sync because the person doesnt have perId set neither, is set on the server and when that call is done ill know the perId that the server responded.

Wait for person.save() to end
i could wait (in some way i dont know) for the save() to return and then set the perId to all phones and do a sync() but that means " go to server to save person, back to client with perId info, go to server to save phones" too much calls when with raw ajax i know i can send all the data in 1 call and save all data .
Anyway this is a solution at least, but i dont know how to wait for save() to return

Use proxy.batch
im reading the proxy.batch() doc, maybe with this i can send the Person model and the phone models in 1 call, but batch will send my Person to its proxy create method (perInsert) and then phones to (phoInsert). Thinking this still.

have hours reading docs but cant find anything, im sad.
any ideas on this ? how to send associated model on record.save() or "in one call to the server" ?
(sorry for my bad english)

28 Dec 2012, 4:21 PM
I have the exact same problem!

I wonder why there is so little support for nested models, when it is a frequent problem.

Also i can get the nested models store by calling
Parent.{child_model_name}(); ----ex. store.phones();

But how can i set those values???

Thank you in advance!

28 Dec 2012, 9:36 PM
If you need to wait for save() to return, you can easily do this:

success: function( records, operation ) {
// response of save is returned...do other stuff

The thing I don't like about this approach is that if the first item gets saved, but the second (in the callback) doesn't, your stuck with a pointless orphaned record.

29 Dec 2012, 5:30 AM
No this isn't a solution.
There will be many cases that i will need to send nested Json Objects on a call, for various reasons.