Friday 15 February 2008

AJAX YAHOO DOMINO - A beginners Guide Part 6

Continuing with the worked example .. the next thing we need is an agent to do the work If you remember back in Part 5 we created a URL that called an agent called getDataLS and that is what you want to create next.

Create an agent and give it the details opposite
and make it a lotusscript agent

Then in the INITIALISE section code something like this

Dim ThisSession As New NotesSession
Dim ThisDoc As NotesDocument
Dim ThisDB As NotesDatabase
Dim ThisView As NotesView
Dim ThisCollection As NotesDocumentCollection
Dim CarDoc As NotesDocument
Dim ParmString As String
Dim ParmArray As Variant
Dim MakeKey As Variant

Set ThisDoc = ThisSession.DocumentContext
Set ThisDB = ThisSession.CurrentDatabase
Set ThisView = ThisDB.getView("autos")

ParmString = ThisDoc.Query_String_DeCoded(0)
ParmArray = Split(ParmString,"=")
MakeKey = Split(ParmArray(1),"&")
Set ThisCollection = ThisView.GetAllDocumentsByKey(MakeKey(1))

Print |Content-Type:text/plain|
Print |CarData.length=0;|
If ThisCollection.count <> 0 Then
Set CarDoc = ThisCollection.GetFirstDocument()
Do While Not (CarDoc Is Nothing)
Print |CarData.push( { Model: "|+CarDoc.Model(0)+|",Year: "|+CarDoc.Yead(0)+|",Price:|+Format(CarDoc.Price(0),"#####0")+|} );|
Set CarDoc = ThisCollection.GetNextDocument(CarDoc)
End If

Lets look at this agent.

The DIMs are doing what DIMs always do
The SETs similarly are SETing things. of note is the view, this is a standard notes view that selected Car forms and has the first column set to the MAKE field and it is sorted

I load a CGI field, these fields are automatically added to the document extractable by the session's documentcontext. Query_string_decoded holds the data passed using the URL
from the ? onward.

Query_String_Decoded(0) in this instance will hold

I then SPLIT this on the & Character into an array called ParmArray
So ParmArray now contains

ParmArray[0] = "openagent"
ParmArray[1] = "key=Ford"
ParmArray[2] = "seed=1234567890"

The bit i am interested in is in ParmArray[1]
So I split this down on the = character into array MakeKey which now contains

MakeKey[0] = "key"
MakeKey[1] = "Ford"

As I now have the Key with which to search my view I create a NotesDocumentCollection
using that key.
 Print |Content-Type:text/plain|
Print |CarData.length=0;|

The lines above are the start of the data that will be returned to the browser. The first line tells the browser to expect plain text. You could set this to use XML at this point, but for the sake of this example we are using plain text.

The second line sets the public array to have no entries (0 length)

Then I loop around the documentcollection and for each document found I return using PRINT a raw javascript statement. If i substitute the field names for values the browser would get something like this

CarData.push( {Model: "Focus",Year: "2005",Price: 2500) );
CarData.push( {Model: "Puma",Year: "2007",Price: 3800) );
CarData.push( {Model: "Granda",Year: "1973",Price: 100) );
CarData.push( {Model: "Ka",Year: "2006",Price: 1500) );

When the loop finishes and all the PRINT statements processed the agent finishes
and the PRINTed data is returned to the browser by the HTTP server.

Because data was successfully received by the browser the CallBack.success code is triggered and the data returned (which is in o.responseText) is "evaluated" which in effect runs all the lines of JS that you sent back from the server.

Once this has happened the DOM now contains a popluated CarData array which you can
do something with which i will talk about in the next post.


Sean Burgess said...

I have done a ton of AJAX agent lately and have learned a thing or 2. First, always include the following at the beginning of my agents:

Print |Content-Type:text/html|
Print |Cache-Control: no-cache|

The cache-control parameter is essential to get the browser not to ignore new data.

Secondly, I find that lists are so much better than arrays for handling QueryString attributes. That way, instead of having to remember which position the seed value is in, you can simply have a list variable called qsparms and access the value as qsparms("seed").

Steve McDonagh said...


Thanks for the comments and yes the Print|Cache-control: no-cache| is something I use on occasions esp if i am guaranteed a unified browser environement. I have had trouble with using it alone with some of the IE's that are out there, particulary Czech and Hungarian V6's for some reason. The packed out unique url, i have found works a treat for these beasts so I kinda settled on that as a method.

So you use lists..hmmm..split the key out and give the key name to the list subscript so you can use
qsparm["model"] to get the value.

OK.. not i feel thick.. LOL.. so simple why didnt i think of that...

Disqus for Domi-No-Yes-Maybe