PDA

View Full Version : Fill record model with id after save



silveralecs
28 Mar 2014, 10:58 AM
Hello,

I've seen in an ExtJs example that if you run MyRecord.save() and the REST returns an object containing the data key like so:



{
success: true,
data: [
{
id: '123456',
field: 'value',
field2: 'value'
}
]
}


It automatically fills that model with id.

1. Where can I find this in the documentation ?
2. Can I change the name of the data key ( ex: instead of 'data' I need 'records' ) ?
3. If I batch insert multiple records ( ex: store sync ) and I return multiple records with the id field prefilled it is ok ? Do I have to return them in the same order as they were inserted, so that ExtJs will know how to correlate the frontend model with the model from the response ?

Thank you very much

Fredric Berling
29 Mar 2014, 1:32 AM
1. Take a look at the json Reader http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.data.reader.Json

2. Yes. Specify the "root" config in the readers config of the proxy



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


3. Yes. The records you return in your json will automatically be realized on the store records because they have the same idīs. The order is not important.

silveralecs
30 Mar 2014, 1:58 AM
Thank you very much for your reasponse. This cleared my questions for 1 and 2 but regarding 3 I still have one little question.

3. I have the following model:



Ext.define('Student', {
extend: 'Ext.data.Model',
fields: [
{name: 'name', type: 'string'},
{name: 'city', type: 'string'},
{name: 'country', type: 'string'}
],
proxy:
// json proxy stuff here
});


And I create 3 new students and add them in a store, and then sync the store, like so:



// as you can see the entries don't have an ID and I also don't add the country.
// the country is determined on the server side, and it comes after it has been saved.

var student1 = Ext.create('Student', { name: 'John', 'city': 'Rome' });
var student2 = Ext.create('Student', { name: 'Foo', 'city': 'Paris' });
var student3 = Ext.create('Student', { name: 'Bar', 'city': 'London' });

// add users to the store
grid.getStore().add(student1);
grid.getStore().add(student2);
grid.getStore().add(student3);

// sync the store
grid.getStore().sync(); // this will send the records in the backend


The backend will respond with something similar to this:



{
success: true,
records: [
{
name: 'John',
city: 'Rome',
country: 'Italy'
},
{
name: 'Foo',
city: 'Paris',
country: 'France'
}
{
name: 'Bar',
city: 'London',
country: 'United kingdom'
}
]
}


How will ExtJs know where to put the corresponding country to each student ? This is why I've asked if I need to return in the same order.

Thank you very much for your time,
I really appreciate it.

Alex

Fredric Berling
30 Mar 2014, 2:05 AM
The "id" of the model is a key thing here.

All records in a database need a unique identifier for the reason you just stated.

You backend database most likely hava some kind of id of the records (rows) in a db table.

You need to send this id along with the data for each record (user) ord find a field that you are certain will be unique.

You can specify which field in the model with the config "idProperty". It defaults to "id".

silveralecs
30 Mar 2014, 1:21 PM
But how can I know which ID will the database give for the specified record ?



// this is how I create models
var model = Ext.create('Ext.data.Model', { id: '???', name:'Foo' });

grid.getStore().add(model);
grid.getStore().sync();


The ID for the specified model will be filled on the response of the request. Do I have to somehow auto-generate the ID on the frontend ?

Thank you

silveralecs
2 Apr 2014, 4:09 AM
Found the answer here: http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.Model-cfg-clientIdPropertyI need to specify the clientIdProperty and also send it in the backend.Thank you