Wednesday 13 February 2008

AJAX YAHOO DOMINO - A beginners guide Part 5

Asking the server for data ...

OK for the sake of this example lets say we want to get some data on Fords from a database of used cars. So we put a button on our web page and when a user clicks a button it will go off to the server and return a list of data about all the documents relating to Ford cars.

First we place a [div] on the page to put the data in followed by a button that the user can click to get the data from the server.
<div id='fordinfo'></div>
<input type='button' onclick="getData('fordinfo','Ford')" value='Get details of Fords'>
Then we go back to the JS header of our page and code up the getdData() function
function getData(target,myKey)
{
uniqueSeed = new Date().getTime();
myCallBack.argument.target=target;
myCallBack.argument.nextFunction="dspCars(o.argument.target);"
url = Location.href.substring(0,location.href.indexOf(".nsf") + 4);
url = url+"/getDataLS?openagent&key="+escape(myKey)+"&seed="+uniqueSeed;
request=YAHOO.util.Connect.asyncRequest("GET", url, myCallBack)
}
Ok what this bit of code is doing line by line
Line 1: uniqueSeed = new Date().getTime();

I build a uniqueSeed value from the current time. I do this so that i can add it to the end of the url so that the URL becomes unique and is never (esp in IE) retrieved from the cache. It plays no part in getting the data other than to ensure "fresh" data.

Line 2: myCallBack.argument.target=target;
I am storing the ID of the DIV i am going to populate so that I can use it later.

Line 3: url = Location.href....
I am building the URL that i am going to pass to the server. This line gets me everthing up to and including the .nsf from the current URL.

Line 4: url = url+"/getDataLS?openagent&key....
I add the agent that I am going to call to the URL along with the key string (which in this example will be FORD along with the unique seed to fool IE into ignoring the cache.

Line 5: request=YAHOO.util.Connect.asyncRequest ...
This is the call to the YAHOO process that will send the request to the server. Note the parms that are passed to it
"GET" means that it is GET rather than a POST operation.
url is the url i have constucted along with the key pairs
myCallBack is the CallBack object I built earlier.

When this function invokes the YAHOO.util.Connect function control returns to the browser and we wait for data to be returned.

...next... the LotusScript Agent

1 comment:

Anonymous said...

Thank you

Disqus for Domi-No-Yes-Maybe