I don’t normally open myself up to this sort of criticism, but after looking all around and not really finding a good example, I came up with this and thought it might save others some time (or at least get them started.)
Enjoy
// Threadsafe Ajax callback example.
// Obtained from http://www.xml.com/cs/user/view/cs_msg/2815
// Author: brockweaver
// This function can be stored in a script library or wherever
// Makes use of Javascript Closures
// Not sure if this will cause an IE memory leak yet.
function ajaxSend(url,callback) {
function ajaxBindCallback() {
if (ajaxRequest.readyState == 4) {
if (ajaxRequest.status == 200) {
if (ajaxCallback){
ajaxCallback(ajaxRequest.response.XML);
}else{
alert("no callback defined");
}
}else{
alert("There was a problem retrieving the xml data:\n" +
ajaxRequest.status + ":\t" + ajaxRequest.statusText + "\n" +
ajaxRequest.responseText);
}
}
}
var ajaxRequest = null;
var ajaxCallback = callback;
if (window.XMLHttpRequest){
//mozilla, etc
ajaxRequest = new XMLHttpRequest();
ajaxRequest.onreadystatechange = ajaxBindCallback;
ajaxRequest.open("GET",url,true);
ajaxRequest.send(null);
}else if(window.ActiveXObject) {
//internet explorer
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
if (ajaxRequest) {
ajaxRequest.onreadystatechange = ajaxBindCallback;
ajaxRequest.open("GET",url,true);
ajaxRequest.send();
}
}
}
/*
Author: Matt Rakestraw
Description:
- Example dblookup simulator for web, called via: ajaxSend(urlStr,lookupLocation);
- The XML processing function is the callback parameter to ajaxSend
- End result is to populate the full name, phone number, and region fields for
the selected store
Lookup field:
Loc - drop down, single selection list
The three target fields on the form are:
LocName - text
LocRegion - text
LocPhone - text
The view is just a lookup view with columns available to return the desired values. In this
example, sorting doesn't matter, and the key must be unique. count=-1 returns all rows.
currNode.childNodes.item(0).text; is the first column value
currNode.childNodes.item(5).text; is the sixth column value
*/
// onchange event of the “Loc” field
urlStr = “/carmax/cmstores.nsf/LStorebyName?ReadViewEntries&count=-1”;
var fld = document.forms[0].Loc;
storeLocStr = fld.options[fld.selectedIndex].text;
function lookupLocation(responseXML) {
this.$storeLocStr = storeLocStr;
if (responseXML) {
//alert("Got some data via ajaxSend.");
}
var response = responseXML.documentElement;
//This seems to work ok as a brute force method...Matt Rakestraw
//obtain handle to the first top level node
var currNode = response.childNodes.item(0);
//search through all the nodes looking for the correct store location
while ((currNode != null) &&
(this.$storeLocStr != currNode.childNodes.item(3).text)) {
currNode = currNode.nextSibling;
}
//process the found node for the matching store information
if (currNode != null) {
this.$storeNameStr = currNode.childNodes.item(0).text;
this.$storeRegionStr = currNode.childNodes.item(5).text;
this.$storePhoneStr = currNode.childNodes.item(6).text;
}else{
this.$storeNameStr = "";
this.$storeRegionStr = "";
this.$storePhoneStr = "";
}
//assign to the correct fields
document.forms[0].LocName.value = this.$storeNameStr;
document.forms[0].LocRegion.value = this.$storeRegionStr;
document.forms[0].LocPhone.value = this.$storePhoneStr;
}
// Call ajaxSend
ajaxSend(urlStr,lookupLocation);