California Ajax Solutions Team

www.CalAST.com

Calling back

So what does this callback function do? Remember that returned packets may be XML, JSON, plain text, or anything else. What the function does depends on the format, and whatever the designer wants it to do. JavaScript is often used with libraries that provide functions for dealing with XML and JSON objects, and you can pretty much figure out plain text yourself. Again, in our example you will receive a list of models, and you will use them to build a new drop-down list for the user.

How do you get the information out of the returned packet? That depends. If the server just returned the ID of something in text form, you just get back "1234" or whatever the answer was. A JSON response is specifically tailored to referencing the result in JavaScript, and you just store it someplace where you can get at it. You then just access it like a big array. XML responses are similar, although not as "lightweight" as JSON. But they are better understood, and you can use normal DOM functions on the returned object, just as if they were HTML instead of XML.

"OK," you say, "I've got the list of models back. Now what?" Basically, you find the existing list, find its parent, remove the child that represents the existing list, build a new "subtree" representing the new list of models, and append it to the parent as a new child. The display will instantly change to reflect the new configuration of the tree. Building the subtree is the hard part.

All this sounds complicated, but it really isn't because JavaScript has built in functions for doing it all. You only need to learn a handful of them, and then you can do pretty much all you need to do. I won't go into them in detail, but the basics involve finding the SELECT element (getElementById("models")) and replacing the options list associated with it. This can be done in several ways, but it's quite intuitive once you play with it for a while. And if you don't like that, you can simply create a big HTML string and assign it to what is called the innerHTML of a node, which lets the browser do the work. It's not as versatile, but it's easier. I'll show you how that looks in the next section.

Next

Headline 3

insert content here