PDA

View Full Version : Two simultaneous ajax requests



nicobarten
10 Nov 2009, 6:45 AM
Hi all.

I have a window, which contains a form and a grid. This window is opened by double clicking on a record in another existing grid. I made it so that in the doubleclick event the window's grid store is reloaded, and after succesful callback the window is shown.

like this:



rowdblclick: function(grid, rowIndex, e)
{
var pers_id = grid.getSelectionModel().getSelected().data.personeel_id;
var datum = grid.getSelectionModel().getSelected().data.datum;
dsHoursPersonel.proxy = new Ext.data.HttpProxy({url: './data/json.php?sort=hours&action=get&pers_id=' + pers_id + '&datum=' + datum});
dsHoursPersonel.reload(
{
callback: function(r, options, success)
{
if(success == true)
{
hoursWindow.show();


In my code the window is called 'hoursWindow' and the grid in the window is attached to a datastore called 'dsHoursPersonel'. This way the window only shows when the data is succesfully loaded.

Now however, i want to fill also the form components through an AJAX request. So i want to simultaneously have 2 AJAX requests, 1 for the grid's datastore, which i already have, and one for the form components. To start both at the same time doesn't seem too difficult for me, but how do i check that both requests are complete?

Condor
10 Nov 2009, 7:02 AM
1. Do NOT change the proxy of a store! Parameters should be set in the store baseParams.

2. You could also request the data for both the form and the grid in a single request (as long as your server supports it). You would need to load the form (or grid) from the grid (or form) load success handler.

3. If you want to sync multiple requests you should create a counter and decrease the counter on every callback. All requests are done when the counter reaches zero.

nicobarten
10 Nov 2009, 7:28 AM
1. Well... i had to, because i send the parameters in the url of the proxy.
2. Guess i'll try this, all the data in one JSON response.

Thanks!

Condor
10 Nov 2009, 7:34 AM
No, really, you CAN'T change the proxy of a store (it breaks proxy event handling)!!!

If you want to change the url of the proxy use the setUrl method.

nicobarten
10 Nov 2009, 7:38 AM
Errrmm i guess i don't understand it fully.

Let's say i have the store, which loads the JSON (including the form data which it doesn't use):



var dsHoursPersonel = new Ext.data.Store(
{
proxy: new Ext.data.HttpProxy({url:'./data/json.php?sort=hours&action=get'}),
autoLoad: false,
reader: new Ext.data.JsonReader(
{
root: 'results',
totalProperty: 'total',
id: 'urenstaat_id'
},
[
{name: 'urenstaat_id'},
{name: 'bedrijf_id'},
{name: 'urensoort'},
{name: 'van'},
{name: 'tot'},
{name: 'omschrijving'}
])
});
Let's say i have in my JSON string the following components: 'results', 'total' and 'otherdata'.

The datastore for the grid only uses 'results' and 'total'. How do i get the 'otherdata' for the form?

Or the other way around: Getting all the jsondata in the AJAX request (for the form), and then somehow use the 'results' and 'total' to store those in the grid's datastore?


Edit: i can't find the setUrl method, is it private, and from which class (HttpProxy?)

Condor
10 Nov 2009, 7:47 AM
Ext 3.x:

store.proxy.setUrl(newUrl);
Ext 2.x:

store.proxy.conn.url = newUrl;

Example:

store.load({
callback: function(){
form.getForm().setValues(store.reader.jsonData.otherdata);
}
});

ps. Ook al last van de Engels/Nederlands mix (results vs. resultaten)?

nicobarten
10 Nov 2009, 11:33 PM
store.reader.jsonData.otherdata
Thanks, this i needed.

----

Ja, ik neem me meestal bij elk project weer voor om alles in het engels te doen, maar als je applicatie helemaal in het Nederlands is... guess what happens... :"> Engels vindt ik voor objectnamen en functienamen meestal makkelijker.