PDA

View Full Version : Response from the server comes later than fires success save callback on model



lufton
9 Jul 2012, 2:19 PM
I want to get auto_incremented id property for my new instance of my model, but when I do this:


address.save({
success: function(record) {
addAddressWindow.record = record;
addAddressWindow.close();
}
});

My window closes earlier then script got response and record variable isn't contains valid id property. Why?
Watch the video, sorry for my pronunciation =)

http://www.youtube.com/watch?v=0_11EXfLdEw

scottmartin
9 Jul 2012, 5:32 PM
Is this an edit or add?

In looking at your video, I noticed the following:

1:05 - No id in your request payload ( should be populated for edit)
1:27 - records.phantom = false (true = new record; if adding)

Does you server generate id?
Do you return success:true?

Do you have root/successProperty set in your proxy/store?

Scott.

lufton
9 Jul 2012, 10:50 PM
This is the handler of my Create button:


var addAddressWindow = Ext.getCmp('addAddressWindow');
var addAddressForm = Ext.getCmp('addAddressForm');
var address = Ext.create('TCIDb.model.Address');
address.set(addAddressForm.getForm().getValues());
if (addAddressForm.getForm().isValid()) {
address.save({
success: function(record) {
addAddressWindow.record = record;
addAddressWindow.close();
}
});
}

Address model has an RestProxy and JsonReader. JsonReader has root propery set to "items".
My server responses just:

{
"id":"27",
"zip":"",
"countryId":"1",
"state":"\u0411\u0440\u0435\u0441\u0442\u0441\u043a\u0430\u044f",
"district":"\u0411\u0440\u0435\u0441\u0442\u0441\u043a\u0438\u0439",
"cityTypeId":"1",
"city":"\u0411\u0440\u0435\u0441\u0442",
"streetTypeId":"2",
"street":"\u041a\u0440\u0438\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0439",
"building":"8",
"housing":"",
"flat":""
}
Should I response:

{
success:true,
items:
{
"id":"27",
"zip":"",
"countryId":"1",
"state":"\u0411\u0440\u0435\u0441\u0442\u0441\u043a\u0430\u044f",
"district":"\u0411\u0440\u0435\u0441\u0442\u0441\u043a\u0438\u0439",
"cityTypeId":"1",
"city":"\u0411\u0440\u0435\u0441\u0442","streetTypeId":"2",
"street":"\u041a\u0440\u0438\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0439",
"building":"8",
"housing":"",
"flat":""
}
}?

lufton
9 Jul 2012, 11:43 PM
Ok, I responsed new record as items element and set success: true, this works, but I have one more problem, when I try to update my record script (I use RestProxy) sends POST request to update model, but here (http://www.yiiframework.com/wiki/175/how-to-create-a-rest-api/) I read that it must send PUT request. You can see:



Request URL:
http://localhost/api/addresses/30?_dc=1341905949020
Request Method:
POST
Status Code:
http://www.sencha.com/forum/chrome-devtools://devtools/Images/successGreenDot.png200 OK
Request Headersview source

Accept:
*/*
Accept-Charset:
windows-1251,utf-8;q=0.7,*;q=0.3
Accept-Encoding:
gzip,deflate,sdch
Accept-Language:
ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4
Connection:
keep-alive
Content-Length:
362
Content-Type:
application/json
Cookie:
PHPSESSID=8b13c59239e1e51312c59df1975de11c
Host:
localhost
Origin:
http://localhost
Referer:
http://localhost/
User-Agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11
X-Requested-With:
XMLHttpRequest

Query String Parametersview URL encoded

_dc:
1341905949020

Request Payload

{"id":"30","zip":"","countryId":"1","state":"\u0411\u0440\u0435\u0441\u0442\u0441\u043a\u0430\u044f","district":"\u0426\u044e\u0440\u0443\u043f\u0438\u043d\u0441\u043a\u0438\u0439","cityTypeId":"1","city":"\u0425\u0435\u0440\u0441\u043e\u043d","streetTypeId":"1","street":"\u041c\u0430\u043a\u0430\u0440\u043e\u0432\u0430","building":"79","housing":"","flat":""}



This is my model definition:


Ext.define('TCIDb.model.Address', {
extend: 'Ext.data.Model',


proxy: {
type: 'rest',
url: '/api/addresses',
reader: {
type: 'json',
root: 'items'
}
},


fields: [
{
name: 'id'
},
...

scottmartin
10 Jul 2012, 8:19 AM
You can override your proxy using:




ex:
actionMethods: {
read: 'GET',
create: 'POST',
update: 'POST', // override PUT
destroy: 'DELETE'
},


Scott.

lufton
10 Jul 2012, 8:30 AM
Thanks, this is usefull