PDA

View Full Version : store.sync() to a perl back-end



ragecf
6 Sep 2013, 12:38 AM
Hi,

This is a question about perl back end.

I want to delete a record from store then sync to back end.
How to pass an additional parameter (action:deleteUser)?

I tried the below code, but perl did not get the action parameter.



Ext.onReady(function () {
var myStore = Ext.create('Ext.data.Store', {
autoLoad: true,
fields:['name','phone'],
proxy: {
type: 'ajax',
api: {
read: 'cgi-bin/cfg.pl?action=getuser',
destroy: 'cgi-bin/cfg.pl?action=deleteuser'
},
reader: {
type: 'json',
root: 'data'
},
writer: {
type: 'json'
}
}
});


Ext.create('Ext.grid.Panel', {
title: 'Grid',
height: 200,
width: 400,
store: myStore,
columns: [
{ text: 'Name', dataIndex: 'name', flex: 1},
{ text: 'Phone', dataIndex: 'phone', flex: 1}
],
dockedItems: [{
xtype: 'toolbar',
items: [{
text: 'Delete',
handler: function() {
var grid = this.up('grid');
var record = grid.getSelectionModel().getSelection()[0];
grid.store.remove(record);
grid.store.sync();
}
}]
}],
renderTo: Ext.getBody()
});
});



And the perl code



#!c:\Perl\bin\perl.exe
use CGI;

my $html=new CGI;
my $action=$html->param('action');

my $data;

if($action eq 'getuser'){
$data="{total:2,data:[{name:'user1',phone:'1234'}, {name:'user2',phone:'5678'}]}";
}
elsif($action eq 'deleteuser'){
$data="{'success':true}";
}

print "\n\n";
print $data;




The getuser function is normal. But the $action is null when action=deleteuser.
Thanks.

Gary Schlosberg
6 Sep 2013, 5:14 PM
Single quotes are not valid in JSON. You can validate JSON here. (http://www.jsonlint.com/) Try changing your 'deleteuser' code line to:


$data='{"success":true}';

ragecf
6 Sep 2013, 7:00 PM
Single quotes are not valid in JSON. You can validate JSON here. (http://www.jsonlint.com/) Try changing your 'deleteuser' code line to:


$data='{"success":true}';


Thanks.

The action=getuser is OK because the read action method is GET.
But can not get the 'deleteuser' because the destroy method is POST. How to get this value?

Gary Schlosberg
6 Sep 2013, 7:30 PM
The CGI.pm (http://search.cpan.org/dist/CGI/lib/CGI.pm#CREATING_A_NEW_QUERY_OBJECT_(OBJECT-ORIENTED_STYLE):) module you are using should give you POST or GET data the same way. My test with your Perl code worked whether I used POST or GET. Perhaps you need to look at the 'enctype' setting on your form:
http://search.cpan.org/dist/CGI/lib/CGI.pm#HANDLING_NON-URLENCODED_ARGUMENTS

ragecf
6 Sep 2013, 10:19 PM
I tried to set "actionMethods:{destroy: 'GET'}" in Ajax proxy. Then the action value could be retrieved. But Ext did not post record to back end.
When I used default "actionMethods:{destroy: 'POST'} in Ajax proxy, the posted record could be read in perl:



#!c:\Perl\bin\perl.exe
use CGI;
use JSON;

my $html=new CGI;
my $post=$html->param('POSTDATA');
my $user=JSON->new->utf8->decode($post)->{'name'};
my $action=$html->param('action');

my $data='{"success":true,'.'"user":"'.$user.'","action":"'.$action.'"}';
print "\n\n"'
print $data;

But return is {"success":true,"user":"user2","action":""}

Sorry for my pool englist. Hope you can see what my mean.

ragecf
6 Sep 2013, 10:22 PM
Or is there is a workaround to post action and record to back end together for store.sync()?

Gary Schlosberg
7 Sep 2013, 10:48 AM
I don't believe you should have to change the actionMethod to GET -- if Ext sends as POST your original Perl script should receive the 'action' parameter the same way, whether GET or POST. You can probably not worry about enctype for now -- I forgot you weren't using a form.

When I run your Ext code, I get a grid with two records. When I select one and click the delete button, the grid refreshes and I only see one record. Isn't that what you were expecting to happen? Is it functioning differently for you?

nohuhu
9 Sep 2013, 1:22 PM
@ragecf: There's no need to use plain CGI interface. Use Direct proxy with CGI::ExtDirect (https://metacpan.org/module/CGI::ExtDirect) instead.

Regards,
Alex.

ragecf
11 Sep 2013, 11:15 PM
"When I run your Ext code, I get a grid with two records. When I select one and click the delete button, the grid refreshes and I only see one record. Isn't that what you were expecting to happen? Is it functioning differently for you?" -------------- Yes, the phenomenon is the same as mine. Then I called store.sync() to tell back end to delete the record from the database. The problem is how to tell the perl script to delete the record.

In another way, when I called store.sync(), Ext auto posted whole record to back-end, but no other variable or symble. So how to tell back end that the posted record shold be deleted, no update or create.

Thank you for your patience.

Gary Schlosberg
12 Sep 2013, 3:46 PM
I get it now -- sorry for missing that. The action=deleteuser is part of the URL and doesn't get POSTed. Well, nohuhu's suggestion to use CGI::ExtDirect is a great suggestion. A quicker and dirtier solution would be to have a different Perl script handle the deletes.

ragecf
12 Sep 2013, 4:41 PM
Thanks a lot.