26 Eylül 2008 Cuma

Asynchronous Ajax in Apex

We use Ajax for rapid query processing without submitting page. In apex, you can build ajax within your pages. For this purposes, we use "Application Process" under the Shared Components.

Sometimes, Application Process can not response rapidly. For example, if you query a count from very large table, it takes for minutes or hours. If you put a text field for table name and if user can query number of records of input big table, also if you perform this operation with ajax in apex as synchronously, the BROWSER will freeze and consumes %100 cpu time. Everything will go bad.

In this situation, you have to use "Asynchronous Ajax" opposite of Synchoronous, so you can more than one job on same time. To perform this, give an eye to this example:

Firstly, you can build your ajax javascript :


function getNumberOfRecords(){
var ajaxRequest = new htmldb_Get(null,&APP_ID.,'APPLICATION_PROCESS=GetCount',0);
ajaxRequest.add('P6_TABLE_NAME',html_GetElement('P6_TABLE_NAME').value);
ajaxRequest.GetAsync(f_AsyncReturn);
ajaxRequest = null;
}


This code is used to connect your apex page with your application process. ( By the way, Application Processes are need to created as On Demand ). In normally, synchronous method, you call get() method of ajaxRequest object. Pay attention to this, in asynchronous method, you need to call GetAsync() method by passing an Object. What is the f_AsyncReturn ? :


function f_AsyncReturn(){
if(p.readyState == 1){
$x('P6_TEXTAREA').value = '';
}else if(p.readyState == 2){
}else if(p.readyState == 3){
}else if(p.readyState == 4){
$x('P6_TEXT_AREA').value = p.responseText;
// you can add other things add this area
}else{return false;}
}


If you coded ajax in other languages such as php,jsp; probably you coded as above. There are 4 states to deal with ajax. One of them (state 1) is "Loading", one of them (state 4) is "Success". Result of ajax is written in 4. states. Before ajax Request is completed, you can write "Loading" message in first state.

The other operations are the same with Synchronous method. You need to create an Application Process, it's running time must be "On Demand" as well.

So, by performing this Asynchronous method, your browser will not be freezed. And, not consuming all of CPU time. But disadvantage of this ( may be not seemed apparently ), your job is take much time rather than synchronous method. But, it is worth!

Hiç yorum yok:

Yorum Gönder