PDA

View Full Version : Ext.Ajax.request ASYNC



caleywoods
5 Sep 2012, 12:00 PM
I have an issue where I'm iterating over an array of selected items from an Ext.grid.Panel using Ext.Array.each(). Inside of Ext.Array.each I am doing an Ext.Ajax.request to determine whether or not each selected item meets the business criterion and then appropriately setting the "dirty" attribute of the record.

You can see my example code here: https://gist.github.com/a83c8a5f10a5fb6fad09

T (https://gist.github.com/a83c8a5f10a5fb6fad09)he issue I have is that since Ext.Ajax.request() is asynchronous the call returns before the records dirty attribute can be flipped to false so my store.sync() executes and the record is modified in the database when it shouldn't be.

How can I cause Ext.Ajax to either block and wait or what is the better solution to this problem? The URL that is called with the ajax request returns true or false for the record depending on if it meets the criterion or not and my if statement on line #23 handles that response logic. If a true is received from the ajax call "data" will be a bool (true) and vice versa.

sissonb
5 Sep 2012, 12:09 PM
You should just send the whole array in one request like,


[
{
invoice_id:"id1",
invoice_number:1234
},
{
invoice_id:"id2",
invoice_number:4321
}
]


The server should give a response like,


[
{
invoice_id:selection.data.invoice_id,
invoice_number:selection.data.invoice_number,
success:false
},
{
invoice_id:selection.data.invoice_id,
invoice_number:selection.data.invoice_number,
success:true
}
]


If you want your current approach to work, you'll need to keep track of which invoices have been validated. In your success method you'll need to check if all invoices have been validated. Once they are all validated you can sync your store in the success method.

caleywoods
5 Sep 2012, 12:43 PM
I'll give that a try. Refactoring the action on the backside to accept an array was on my refactor list but it looks like it will be sooner rather than later.

caleywoods
6 Sep 2012, 7:39 PM
@sissonb: Before I venture into refactoring my ajax action on the server to accept an array, I tried using Ext.data.Model.save() to save that individual record to the store but I'm getting an error of 'unable to call method indexOf on undefined'. Any ideas there?

caleywoods
10 Sep 2012, 6:54 AM
I've refactored the action to accept an array and i'm examining the total on the JS side and the success callback is correctly setting the dirty attrib of each passed invoice based on business logic. When i try and sync the store now it throws an 'unable to call method indexOf on undefined'.

new code: https://gist.github.com/84c51ef13b72a4c4416d

sissonb
11 Sep 2012, 11:23 AM
I cannot see the definition of the invoicer_unfinaledInvoiceStore, but if you look at the MVC tutorial (http://docs.sencha.com/ext-js/4-1/#!/guide/application_architecture) it should help you with syncing your store. Especially the section called, Saving to the server. You probably need to define the api for your store's proxy.