AJAX In Action [171]
handleAjaxResponse: function(request) {
if ( request.readyState == net.READY_STATE_COMPLETE ) {
if ( this.isSuccess(request) )
this.component.ajaxUpdate(request);
Message component
else
with response
this.component.handleError(request);
Message component
}
with error
},
isSuccess: function(request){
return request.status == 0
|| (request.status >= 200 && request.status < 300);
}
All the method does is check for the appropriate readyState of 4 (indicating completion) and notifies the this.component that the response is available. But we’re not quite finished yet. The other requirement we said we would address is to handle errors appropriately. But what is appropriate? The point is, we don’t know what’s appropriate. How to handle the error is a decision that should be deferred to another entity. Therefore we assume that our client, this.component, has a handleError method that takes appropriate action when the Ajax response comes back in a way we didn’t expect. The component may in turn delegate the decision to yet another entity, but that’s beyond the scope of what we care about as a helper object. We’ve provided the mechanism; we’ll let another entity provide the semantics. As mentioned earlier, we’re assuming that this.component has an ajaxUpdate and a handleError method. This is an implicit contract that we’ve created, since JavaScript isn’t a strongly typed language that can enforce such constraints.
Licensed to jonathan zheng 352 CHAPTER 9 Dynamic double combo Congratulations! You’ve morphed net.ContentLoader into a flexible helper to do all the Ajax heavy lifting for your Ajax-enabled DHTML components. And if you have a DHTML component that’s not yet Ajax-enabled, now it’ll be easier! Speaking of which, we have a double-combo component to write. 9.6.2 Creating a double-combo component We’ve laid some groundwork with our net.ContentLoader to make our task here much easier, so let’s get started. Let’s assume that our assignment as a rock-star status developer is to create a double-combo script that can be reused in many contexts across an application, or many applications for that matter. We need to consider several features in order to meet this requirement: ■ Let’s assume that we may not be able or want to directly change the HTML markup for the select boxes. This could be the case if we are not responsible for producing the markup. Perhaps the select is generated by a JSP or other server-language-specific tag. Or perhaps a designer is writing the HTML, and we want to keep it as pristine as possible to avoid major reworks caused by a round or two of page redesigns. ■ We want a combo script that is able to use different URLs and request parameters to return the option data. We also want the design to accommodate further customization. ■ We want to be able to apply this double-combo behavior potentially across multiple sets of select tags on the same page, also potentially setting up triple or quadruple combos, as discussed earlier. Starting from the perspective of our first task, keeping the HTML markup as pristine as possible, let’s assume the markup shown in listing 9.10 is representative of the HTML on which we will be operating. Listing 9.10 Double-combo HTML markup listing Licensed to jonathan zheng Refactoring 353 What we need is a DoubleCombo component that we can attach to our document to perform all of the double-combo magic. So let’s work backwards and consider what we would want our markup to look like; then we’ll figure out how to implement it. Let’s change the markup to look something like listing 9.11. Listing 9.11 Double-combo HTML modified markup listing ...
-->