AJAX In Action [243]
this.xsltURL = xsltURL;
this.setOptions(options); b
Configure the component
var oThis = this;
lookupField.form.onsubmit = function(){
oThis.doSearch(); return false; };
this.initialize();
Go to previous search
}
The first four arguments of our constructor are the things we just indicated we’d have to keep up with: the URL of the page, the search field, and the two document URLs. The last parameter is our familiar options parameter for providing component configurability. The options argument is passed to the setOptions() method, which, as you recall, provides some default values for all configurable data b. Let’s look briefly at the defaults before moving on:
setOptions: function(options) {
this.options = options;
if ( !this.options.loadingImage )
this.options.loadingImage = 'images/loading.gif';
if ( !this.options.bookmarkContainerId )
this.options.bookmarkContainerId = 'bookmark';
if ( !this.options.resultsContainerId )
this.options.resultsContainerId = 'results';
if ( !this.options.bookmarkText )
this.options.bookmarkText = 'Bookmark Search';
},
The setOptions() method is not as succinct as its counterpart in the TextSuggest component (see chapter 10), which used the Prototype library’s extend() method to make the expression of this nice and compact. This method performs the same chores, however, and provides default values for the loading image, the ID of the element to contain the bookmark, the ID of the element to contain the result data, and, finally, the text of the generated bookmark. Recapping the mechanism, any values to these properties that live in the options object passed in by the user will override the defaults that are specified here. The resulting options object is a merged set of defaults and their overrides in a single object. These options are then used at the appropriate points throughout the rest of the example to configure the component. Licensed to jonathan zheng 498 CHAPTER 12 Live search using XSLT With component configurability and defaults squared away, let’s turn our attention back to the constructor for a moment and recall these two unassuming lines of code: var oThis = this; lookupField.form.onsubmit = function(){ oThis.doSearch(); return false; }; You will recall that our script in its original form modified the HTML by putting an onsubmit handler on the search form: