PDA

View Full Version : How can I keep a JsonStore RESTful even after adding a writer?



jbrendel
30 Aug 2010, 1:36 PM
Hello!
Normally, a JsonStore retrieves data with a GET request. However, in the moment I add a JsonWriter to the store, the old GET request turns into a POST, with "xaction=read" in the body. Can I prevent this from happening? I would much rather stay RESTful: GET to retrieve data, POST to create data, ideally PUT to update data, and so on.

Is there a setting I can fiddle with to tell Ext not to send POST messages with 'xaction=read'?

Thank you very much...

Juergen

jarrednicholls
30 Aug 2010, 3:19 PM
Hi Juergen,

Have you set the "restful" flag to true? That will post in a true restful (resourceful) fashion. If your endpoints cannot work as such, you can try passing { method: 'GET' } to your store.load() calls. I don't believe there's a way to configure the writer actions to do a particular HTTP method otherwise unless it is a restful proxy. If it is restful, then the Ext.data.Api.restActions are used http://dev.sencha.com/deploy/dev/docs/?class=Ext.data.Api&member=restActions

jbrendel
30 Aug 2010, 3:34 PM
Hello!

Thank you for your reply. Yes, I had just found out about the HttpProxy, which apparently I need to add to my store. Once I do that, it definitely gets better. The store was created in the Designer, so adding the proxy looks like this:



new MyStore({
proxy: new Ext.data.HttpProxy({
restful:true,
api:{
read:{ url:'/resource/AllCustomer/entries', method: 'GET' },
create:{ url:'/resource/AllCustomer/entries', method: 'POST' }
}
})
});

But as you can see it now seems as if I need to specify the URI for every method. However, I had already specified the URI when I created the store in the Designer. Is there a way to define the API so that it just uses the URI that's already specified for the store, without having to repeat myself?

Furthermore, the update and delete action should go to /resource/AllCustomer/entries/{id}, where the value for 'id' is taken from a particular column (I'm using an EditorGridPanel). How would I go about specifying that?

Thank you again for your response...

Juergen

jarrednicholls
30 Aug 2010, 4:11 PM
Hi Juergen,

I do believe that if you omit the url config, then the proxy's URL will be used:



new Ext.data.HttpProxy({
restful: true,
url: '/resource/AllCustomer/entries',
api: {
read: 'GET',
create: 'POST',
// etc.
}
})


Also, the restful Api actions are already configured to the following:


{
create : 'POST',
read : 'GET',
update : 'PUT',
destroy : 'DELETE'
}


So if you use the same HTTP methods as described above, then you should only have to set restful: true, and url: '...' and that's it.

jbrendel
30 Aug 2010, 4:37 PM
Hello!

Yes, defining URL once in the proxy specification is sufficient. However, I am creating the grid from within the Designer. So, the URL is defined as a property of the grid. There doesn't seem to be a way to create a proxy from within the designer, or just to tell the proxy that it should use the URL from the grid definition?

The other thing I noticed is that even though I edit only a single field in the grid, the server receives a POST message. However, it should have been a modification of an existing records, which should have resulted in a PUT message, right?

What did I do wrong?

Juergen

jarrednicholls
31 Aug 2010, 5:01 AM
Hey Juergen,

The URL is placed on the Store, not the Grid. I believe that's what you meant. When the URL is on the Store, it is copied to a proxy under-the-hood. Likewise, the Store can accept a "restful" configuration option to make the proxy restful under-the-hood. You can configure "url" and "restful" within the designer on your JsonStore.

As for your second issue: a POST is sent when the record is sending a "create" action, rather than an "update" action. The records decide that they are being updated or created via a "phantom" flag. The phantom flag is toggled TRUE when the record was created and does not have an "id" that is set from the server. If you are populating the Store from the server, make sure you are returning the "id" of the records. If there is an "id" or PK set on the record, then it will be sent as a PUT/update action.

jbrendel
1 Sep 2010, 10:46 AM
Hello Jarred,

Thank you for your response. Yes, that seems to work better now. I still need to add a writer manually, but by setting the 'restful' flag on the JsonStore as well as the 'idProperty', I now get PUT requests, with the ID attached to the store URI, which is what I need. I also don't need to specify the HttpProxy manually.

However, one problem remains: In the PUT request, the records is sent as { 'undefined' : { <... record ...> } }

Where does the 'undefined' come from? Can I remove it altogether? I only need the actual { <... record ...> } to be contained in the request body.

Thank you very much...

Juergen