PDA

View Full Version : store ajax serving multi read requests (best practice question)



johnzen
26 Aug 2015, 4:48 AM
Hi, im trying to understand the proper design concept of Store proxies using the Ajax api config and have a question on how this should be done.

suppose i have a store which has a server proxy using the Ext.data.proxy.Ajax class and i have an api with the following:



proxy: {
type: 'ajax',
api: {
read: 'some/something/list.json',
create: 'some/something/insert.json',
update: 'some/something/update.json',
destroy: 'some/something/destroy.json'
}
}


now suppose my read is triggered by a search button and when i have a blank text box and click search it makes a request through the read api to retrieve the list.json. but i want to have another read as part of the same store / api to read individual records say something like this:


read: 'some/something/<field_value>.json'

my proxy read is already assigned to the list.json but i want to allow the same store proxy to be able to read from individual record searches also. granted that i cant have two read statements in my proxy. how would I go about writing this?

help me understand? maybe my server controller has to be able to determine by keyword @PathVariable if path is list i.e <list>.json then call the list db query otherwise if <field_value>.json then run the individual search query through the db.?? and if so what would be the read: url?

whats the best way to design and build this?

Thanks in advance

joel.watson
29 Aug 2015, 11:50 AM
Hi--

As with most things, "best" is a bit elusive and very dependent on the context :)

In the past, I've done something similar by leveraging the Rest proxy. It has the same concept of api routes, but also leverages the HTTP verbs based on request type. One really nice feature of the Rest proxy, however, is that it can also modify the endpoint url based on a pattern. So for example, the default "read all" route might be "/users.json", but the "read this record" url can use the same "root", but add the instance id as well: "/users/someid.json". This, of course, presumes that your application server is setup to understand RESTful urls, but I've found it to be a really nice pattern.

Of course, you could also just continue using the Ajax proxy, and add a param to the request which could then be parsed by the server. So your endpoint (/users.json) could be the same, but differentiate the type of query executed against the database (or whatever storage mechanism you're using) depending on whether or not that param (or any other param, for that matter) exists.

So, options! What's best will mostly depend on what works best within your infrastructure, but Ext JS is flexible enough to accommodate either (or other) approach.

I hope that helps--thanks!
Joel