Online Book Reader

Home Category

AJAX In Action [241]

By Root 3921 0

XSLTHelper.isXSLTSupported();

rather than having to instantiate an object like this:

Licensed to jonathan zheng

Refactoring

493

var helper = new XSLTHelper( 'phoneBook.xml',

'transformation.xsl' );

var canDoThis = helper.isXSLTSupported();

So let’s accommodate our inquisitive users with a pseudo-static method, which is expressed as follows:

XSLTHelper.isXSLTSupported = function() {

return (window.XMLHttpRequest && window.XSLTProcessor ) ||

XSLTHelper.isIEXmlSupported();

}

XSLTHelper.isIEXmlSupported = function() {

if ( ! window.ActiveXObject )

return false;

try { new ActiveXObject("Microsoft.XMLDOM");

return true; }

catch(err) { return false; }

}

There’s nothing new here. The logic is identical to the logic defined earlier; we’ve just encapsulated that knowledge about how to detect XSLT support. I’m sure someone will thank us for this. So now we can get on to the business of fleshing out the rest of the XSLTHelper API.

Let’s keep things simple. How about saying that we’ll have a single method for clients of our class to call to perform XSLT processing? Our helper will have ancillary methods to separate the responsibilities of all the internal logic, but we’ll provide a single API for our clients to use. The semantics will be as follows: var helper = new XSLTHelper ( 'phoneBook.xml',

'transformation.xsl' );

helper.loadView( 'someContainerId' );

In this example usage, the phoneBook.xml document should be transformed into HTML by the transformation.xsl document, and the resulting HTML should be placed within the element whose ID is someContainerId. Let’s further specify that the parameter to loadView() can be either a string representing the ID of an element or the element itself. We’ll internally figure out what we’re dealing with and react accordingly. And, by the way, if the client doesn’t care to reuse the helper instance, we can express all this with a single line of code: new XSLTHelper('phoneBook.xml',

'transformation.xsl').loadView('someContainerId');

Now that we’ve defined our API and its semantics, let’s implement it as shown in listing 12.13.

Licensed to jonathan zheng

494

CHAPTER 12

Live search using XSLT

Listing 12.13 The loadView method

loadView: function ( container ) {

if ( ! XSLTHelper.isXSLTSupported() )

b Check for XSLT

return;

support

this.xmlDocument = null;

c Reinitialize

this.xslStyleSheet = null;

helper state

this.container = $(container);

new Ajax.Request( this.xmlURL, d

Request documents

{onComplete: this.setXMLDocument.bind(this)} ); new Ajax.Request( this.xslURL,

{method:"GET",

onComplete:

this.setXSLDocument.bind(this)} );

},

The first thing the loadView() method does is makes sure it’s operating within a browser runtime that supports XSLT b. The client should have already done this, as in our earlier example, but just in case the user of our code is sloppy, we take a better-safe-than-sorry approach and check again. Second, the method sets the state variables holding the XML and XSL documents to null and sets the reference to the container to be updated c. Lastly, we send the Ajax requests to retrieve the XML and XSL documents d. When the server responds to the request for the XML document, the setXMLDocument() method is called. Likewise, when the server responds to the request for the XSL document, the setXSLDocument() method is called. These functions are shown in listing 12.14. Listing 12.14 Setting the XML and XSL documents

setXMLDocument: function(request) {

this.xmlDocument = request.responseXML;

this.updateViewIfDocumentsLoaded();

},

setXSLDocument: function(request) {

this.xslStyleSheet = request.responseXML;

this.updateViewIfDocumentsLoaded();

},

Licensed to jonathan zheng

Refactoring

495

These methods set the state variables of the XSLTHelper corresponding to the XML document and XSL document, respectively. They then call the updateViewIfDocumentsLoaded() method, which checks to see if both documents have been initialized, and if this is the case, updates the view.

Return Main Page Previous Page Next Page

®Online Book Reader