Online Book Reader

Home Category

AJAX In Action [225]

By Root 4185 0
is valid JavaScript code. The thing that’s very desirable about this kind of approach is that the client doesn’t have to do any data-marshaling or parsing to grok (geek-speak for understand) the response. The response is simply evaluated via the JavaScript eval() method, and the client is absolved from all further responsibility. The negative side of this approach is that it puts the responsibility on the server to be able to understand the client-side object model and generate a syntactically correct language-specific (JavaScript) response. The second downside of this approach is partially addressed by the popular variety of this technique of using JSON to define our responses. There are some server-side libraries that aid in the generation of JSON responses (see chapter 3), although these are moving more toward what we described in chapter 5 as a data-centric approach.

For now, we’re going to stick to a script-centric system, so let’s look at our implementation and see what we can do to help it along. Let’s start with our ajaxUpdate() function and its helper runScript(): ajaxUpdate: function(request) {

this.runScript(request.responseText);

},

runScript: function(scriptText) {

eval(scriptText);

},

As already discussed, the response handling is simple to a fault. All we do is call the runScript() method with the responseText, and the runScript() simply eval()s the response text. So why, you might ask, don’t we just get rid of the runScript() method altogether and just call eval() from within the ajaxUpdate() method? Well, that’s certainly a valid and useful approach. It might be nice, however, to have a method that encapsulates the concept of running a script. For example, what if we added a preprocessing step or a postprocessing step to our runScript() implementation? Again, we’ve isolated a change point. Our ajaxUpdate() method is happily oblivious of the change, and we pick up the new behavior. One interesting application of this technique would be a preprocessor that does token replacement of values that reside on the client before executing. Finishing out our Ajax discussion with the ever-important handling of errors, let’s show our handleError() method. Recall that just as the ajaxUpdate() method is an implied contract required for collaboration with the net.ContentLoader, so is the handleError(). The handleError() method is shown here: Licensed to jonathan zheng

464

CHAPTER 11

The enhanced Ajax web portal

handleError: function(request) {

if ( this.options.messageSpanId )

document.getElementById

(this.options.messageSpanId).innerHTML =

"Oops! Server error. Please try again later.";

},

This method checks for the existence of the messageSpanId configuration property and, if it exists, uses it as the element to place an “Oops!” message onto the UI. The actual text of the message that’s presented is something that could also be parameterized with the options object. This is an exercise left to the reader. With that, our portal component refactoring session has come to a close. We’ve created a deceptively simple mechanism for providing Ajax portal management. Now let’s take a few moments to review the focus of our refactoring and recap what we’ve accomplished.

11.6.5 Refactoring debrief

In a couple of ways, the development of this component is quite different than the other component examples in this book. First, the portal component is a more coarse-grained component for providing an Ajax-based portal capability. A third-party developer is unlikely to want to drop a portal system into the corner of his page! Second, it uses a technique for handling Ajax responses as JavaScript code. Our refactoring of this component focused on ways to isolate change points. This was illustrated in several ways:

We provided a clean way to adapt the AjaxWindows.js library.

We isolated string literals as pseudo-constants.

We wrote a generic method for issuing commands.

We isolated via a method the concept of running an Ajax response script. 11.7 Summary

The portal can be one of the

Return Main Page Previous Page Next Page

®Online Book Reader