PDA

View Full Version : update/create actions and DirectStore



sancho_0
25 Oct 2009, 8:25 AM
Hi,

I'm trying to implement the example in http://www.extjs.com/deploy/dev/examples/writer/writer.html using a DirectStore and Ext.Direct functions in the api property, but I can't figure out what should return the server in the update/create actions.

The same issue it's described in this post: http://www.extjs.com/forum/showthread.php?p=391510#post391510

The first answer to that post, it works to avoid the Ext.data.DataReader exception, but if the server returns that, it's no possible to return anything else than the records updated. If I need to return any message or something like that, I can't.

Does anyone implement something like this?? (a writer enabled DirectStore)

Best Regards

sancho_0
14 Nov 2009, 4:14 PM
Hey, nobody??

I'm still looking for this...

All I found it's that I should overwrite the functions onCreateRecords/onUpdateRecords in Ext.data.Store,



onCreateRecords : function(success, rs, data) {
if (success === true) {
try {
this.reader.realize(rs, data);
this.reMap(rs);
}
.....
.....
}
},
to



onCreateRecords : function(success, rs, data) {
if (success === true) {
try {
this.reader.realize(rs, data[this.reader.meta.root]);
this.reMap(rs);
}
.....
.....
}
},
am I right?? could this modification, break something else??

HELP PLEASE!!!

sancho_0
16 Nov 2009, 6:34 PM
Hey... it's me again...

I also found a problem when the create/update action fails due to a server validation or something like that.

The 'onWrite' function, in Ext.data.DirectProxy seems to be incomplete or something...


onWrite : function(action, trans, result, res, rs) {
this.fireEvent("write", this, action, result, res, rs, trans.arg);
trans.callback.call(trans.scope, result, res, true);
}At least, with this code, if the server returns successProperty===false, it doesn't matter..., the "write" event it's fired anyways and the callback it's called with 'true' as the last parameter...

Looking the 'onWrite' function in Ext.data.HttpProxy, I guess that changes need to be made are


onWrite : function(action, trans, result, res, rs) {
var records;
try {
records = trans.reader.readRecords(result);
} catch (e) {
this.fireEvent('exception', this, 'response', action, trans, res, e);
trans.callback.call(trans.scope, result, res, false);
return;
}

success = result[trans.reader.meta.successProperty]
if (success === false) {
this.fireEvent('exception', this, 'remote', action, trans, result, rs);
} else {
this.fireEvent("write", this, action, result, res, rs, trans.arg);
}
trans.callback.call(trans.scope, result, res, success);
}it's this a bug? am I loosing of something??

Best Regards

cerad
6 Jan 2010, 1:57 PM
I ran in to the same issue in 3.1.0.

Ext.data.DirectProxy seems to be ignoring the success flag.

The fix works but I'm not convinced that being unable to create/update a record should be considered "exceptional". Lots of reasons the application might decide not to update.

Seems like there should be a success/failure handlers of some sort.

maxigroovy
12 Jan 2010, 7:11 AM
I just can't get the create action to fire the 'write' event.
It just does not find the method. Also the dirty flag is not cleared.

On Update and Destroy there is no problem. Only the data / result value is not good extracted (on neither Update nor Create)
The returning json is the same in update or create:

Update result:

{"type":"rpc","tid":12,"action":"Management","method":"UpdateWorkers","result":{"success":true,"data":[{"NameID":"Kasper 4","Name":"Kasper 41","PhoneNumber":"1234 ","WorkScheduleName":"shsdfhsdfh"}],"total":1}}

Create result:

{"type":"rpc","tid":10,"action":"Management","method":"CreateWorkers","result":{"success":true,"data":{"workers":[{"NameID":"ext-record-1","Name":"Kasper 4","PhoneNumber":"1234","WorkScheduleName":"shsdfhsdfh"}]},"total":1}}

I'm using ExtJS 3.1 and ExtDirect for ASP.Net MVC 0.8.2.

Any ideas?

tarini
28 Jan 2010, 2:18 AM
Hi to all,
I've got the same problem in create/update... here's my request/response transactions

create:

extjs request:


{"action":"MovieService","method":"create","data":[{"entityList":[{"title":"title","year":1900,"summary":"summary"}]}],"type":"rpc","tid":3}


server response:


{"result":{"entityList":[{"title":"title changed by server to test","year":"1900",
"summary":"summary","id":100}],"total":1},"tid":3,"action":"MovieService","method":
"create","type":"rpc"}


it doesn't work because my JsonReader cannot find data property to "realize" phantom record -_-
(it throws Ext.data.DataReader: #realize was called with invalid remote-data. Please see the docs for DataReader#realize and review your DataReader configuration.)

update:

extjs request:


{"action":"MovieService","method":"update","data":[{"entityList":[{"title":"new title","year":1994,"summary":"Summary","id":3}]}],"type":"rpc","tid":5}

server response:


{"result":{"entityList":[{"title":"new title changed by server to test","year":
"1994","summary":"summary","id":3}],
"total":1},"tid":5,"action":"MovieService","method":"update","type":
"rpc"}


in this case record is flagged as updated (the red triangle from grid is removed) but title doesn't change


where I am wrong?

thanks

tsedeke
5 Feb 2010, 8:15 AM
Have the same problem as described above. Tracing the post and response from example page above.

I got it working.

For 'create' you need as response the full record from inserted record. This will be also good for update, cause databases often has triggers which change the entry values. So in PHP the response can look like this:


$datarow = $zfDBtable->find('<recordID>')->asArray();
$res = array(
'success' => true,
'message' => 'insert/update done',
'dataroot' => $datarow
);
echo json_encode($res);


This is just an example. The values 'dataroot' must be change to your settings in ext store object. I use here also the Zend Framework for database activities.

Hope that help's someone.

khebs@live.com
10 Feb 2010, 4:43 PM
Have the same problem as described above. Tracing the post and response from example page above.

I got it working.

For 'create' you need as response the full record from inserted record. This will be also good for update, cause databases often has triggers which change the entry values. So in PHP the response can look like this:


$datarow = $zfDBtable->find('<recordID>')->asArray();
$res = array(
'success' => true,
'message' => 'insert/update done',
'dataroot' => $datarow
);
echo json_encode($res);


This is just an example. The values 'dataroot' must be change to your settings in ext store object. I use here also the Zend Framework for database activities.

Hope that help's someone.

Hi, did you apply any patches/overrides in Ext.Direct? i tried this it didnt worked for me, would you be so kind to post a sample post & response value? did the successProperty recognized when it is False in your code?

cerad
10 Feb 2010, 5:46 PM
Hi, did you apply any patches/overrides in Ext.Direct? i tried this it didnt worked for me, would you be so kind to post a sample post & response value? did the successProperty recognized when it is False in your code?
http://www.extjs.com/forum/showthread.php?t=91793

As far as I can tell, ignoring the successProperty is a feature not a bug.

tsedeke
11 Feb 2010, 12:27 AM
Right it ignore the Success property. Think you have to read it self and react.
This is not necessary for me. When I got a database error I send the hole error or I create my own error in Zend Framework. So I do not send the row back but the error.
Im my datastore I have a exception listener:

listeners: {
exception: function(proxy, type, action, options, response, arg){
Ext.Msg.show({
title : 'REMOTE EXCEPTION',
msg : response.responseText,
icon : Ext.MessageBox.ERROR,
buttons : Ext.Msg.OK
});
}

tsedeke
11 Feb 2010, 12:29 AM
My response for create record show follow:

{"success":true,"message":"Record created","data":[{"adr_id":"16","adr_desc":"Neue Adresse","adr_adress1":null,"adr_address2":null,"adr_zip_code":null,"adr_town":null,"adr_phone":null,"adr_fax":null,"adr_mail":null,"adr_web":null,"adr_default":"0","sup_id":"9"}]}

khebs@live.com
11 Feb 2010, 4:03 AM
I see, so if you have encountered an error along the way, for example on grid, you should push/trigger an Exception not by returning (success: false), anyway thanks, i know Ext.Direct is not that yet well tested..

xp743
7 Jun 2010, 9:41 AM
Try change your json response from :

"data":[{"adr_id":"16","adr_desc":"Neue Adresse","adr_adress1":null,"adr_address2":null,"adr_zip_code":null,"adr_town":null,"adr_phone":null,"adr_fax":null,"adr_mail":null,"adr_web":null,"adr_default":"0","sup_id":"9"}]

to :

"data":{"adr_id":"16","adr_desc":"Neue Adresse","adr_adress1":null,"adr_address2":null,"adr_zip_code":null,"adr_town":null,"adr_phone":null,"adr_fax":null,"adr_mail":null,"adr_web":null,"adr_default":"0","sup_id":"9"}

And to get all message working :

Ext.data.DataProxy.on({
'beforewrite':{fn:function(){your func show message before send data to write}},
'write':{fn:function(){your func show message after do write success true /false}},
'exception':{fn:function(){your func show message when your json message first array with : {"type":"exception",etc}}},
})

all my settings like above work well for me with Directsore+restful+phpMVC(ORM)