Sending Results to the Client
As I just showed in Example 16-5, the server will take care of creating the XHTML to send to the client so that the hints will appear to the user as quickly as possible. This is accomplished by taking the responseText of the server's response and immediately setting it to the innerHTML of the <div> element before making the <div> element visible.
Each hint result that is passed to the client must be capable of submitting the search form to the server. An onclick event should be set for each of these, along with the search string to use. When the user clicks on the results, the corresponding search string is sent to the server as though the user had clicked the Search button that accompanies a form. Example 166 shows the function to handle this event.
Example 16-6. Submitting a search from hints
Code View:
<script type="text/j avascript"> //<![CDATA[
/* Example 16-6. Submitting a search from hints. */ /**
* This function, executeSearch, sends a search request to the server
* for the passed /p_searchString/ and places the results in the /innerHTML/
* @param {String} p searchString The string that is to be searched for.
* @return Returns false so that the click event does not follow through.
* @type Boolean
* @see Ajax#Request
function executeSearch(p searchString) {
/* Is there anything to search for? */ if (p_searchString != '')
new Ajax.Request('search.php', { method: 'post', parameters: { searchString: p searchString }, onSuccess: function(xhrResponse) { /* Did we get any results? */ if (xhrResponse.responseText == 0)
else
$('myResults').innerHTML = xhrResponse.responseText;
return (false);
Google is perhaps the most popular search engine on the Web today, and it has developed some of the most innovative ideas using Ajax. One of the secrets to Google's success has been giving developers access to their solutions through an API (http://code.google.com/apis/ajaxsearch/ ). By doing this, Google allows developers to add a search box to a site that ties directly to Google's search engine, encouraging more people to use Google for their searching needs. And the cost for this ability? Nothing.
16.3.1. Google's AJAX Search API
Google provides many ways to conduct Ajax searches through its API besides the raw searching capabilities we saw earlier in this chapter. Google allows you to search in its different search categories via different predefined objects. Depending on what you want your search to do, Google offers a more generic search control as well as specialized controls. Table 16-1 lists these specialized Searchers.
|
Searcher |
Description |
|
GSearch |
The GSearch object provides the ability to execute searches and receive results from a specific search service. This is the base class that the service-specific searchers inherit from. |
|
GwebSearch |
The GwebSearch object implements a Gsearch interface for the Google Web Search service. It returns a collection of GwebResult objects upon search completion. |
|
GlocalSearch |
The GlocalSearch object implements a Gsearch interface for the Google Local Search service. It returns a collection of GlocalResult objects upon search completion. |
|
GvideoSearch |
The GvideoSearch object implements a Gsearch interface for the Google Video Search service. It returns a collection of GvideoResult objects upon search completion. |
|
GblogSearch |
The GblogSearch object implements a Gsearch interface for the Google Video Search service. It returns a collection of GblogResult objects upon search completion. |
|
GnewsSearch |
The GnewsSearch object implements a Gsearch interface for the Google News service. It returns a collection of GnewsResult objects upon search completion. |
|
GbookSearch |
The GbookSearch object implements a Gsearch interface for the Google Book Search service. It returns a collection of GbookResult objects upon search completion. |
In this chapter, I will concentrate on a few objects. 16.3.1.1. GSearchControl
The GSearchControi object is a single search control on a page that is a container for Searchers. This object is not functional until it has at least one searcher child. A search control is bound to an XHTML container using its draw( ) method. The GSearchControl acts as the holder for a set of Searcher objects that can be manipulated or used on the client.
There are three steps to making this object functional, and they have an expected order of completion:
1. Create a new instance of the GSearchControl object using sc = new GSearchControl( ).
2. Add a Searcher or multiple Searchers to the object using sc.addSearcher( ) .
3. Draw the control so that it is ready for use using sc.draw( ).
When these steps have been executed, the search control is ready for use.
Searcher objects may not be added to a search control once its draw( ) method has been called.
Table 16-2 shows the methods that are available to the GSearchControl object.
|
Method |
Description |
|
addSearcher(searcher [,options]) |
The method addSearcher( ) adds a Searcher object to the control. The optional options parameter supplies configuration options for the passed searcher. |
|
cancelSearch( ) |
The method cancelSearch( ) is used to tell the search control to ignore all incoming |
clearAllResults( )
The method clearAllResults( ) is used to clear all of the search results from the search control.
draw(element[, options]
The method draw( ) activates the control by creating the user interface and search result containers for each configured searcher. The element must be an XHTML block element, while the optional options supplies a GdrawOptions object that can be used to specify either linear or tabbed drawing mode.
execute([query]
The method execute( ) causes the search control to initiate a sequence of parallel searches across all configured searchers. When the optional query argument is supplied, its value is placed within the search control's input text box and becomes the search expression. When this method is called, all previous search results are cleared.
inlineCurrentStyle(node[, deep])
The method inlineCurrentStyle( ) is a static utility method used to clone the current computed style for the specified node (or tree of nodes when the optional deep is set) and inline the current style into the node.
setLinkTarget(target)
The method setLinkTarget( ) sets the target used for links embedded in the search results. Valid values are:
• GSearch.LiNK_TARGET_BLANK: Links will open in a new window. This is the default value for the control._
• GSearch.LINK TARGET SELF: Links will open in the same window and frame.
• GSearch.LiNK_TARGET_TOp: Links will open in the topmost frame.
• GSearch.LiNK_TARGET_PARENT: Links will either open in the topmost frame, or replace the current frame.
• Anything else: Links will open in the specified frame or window.
setOnKeepCallback(object, method[, keepLabel])
The method setOnKeepCallback( ) is used to inform the search control that the caller would like to be notified when a user has selected a text link for copy. When called, each search result is annotated with a text link, underneath the search result; when clicked, this will cause the methc d to be called, passing it a GResult object containing search results. The object defines the context in which the method will be called, while the optional keepLabel supplies an optional text label to be used for clicking. Valid values include:
• GSearchControl.KEEP_LABEL_SAVE: A label value of "save."
• GSearchControl.KEEP LABEL KEEP: A label value of "keep."
GSearchControl.KEEP LABEL INCLUDE: A label value of "include.
• GSearchControl.KEEP_LABEL_COPY: A label value of "copy." This is the default value for the label.
• GSearchControl.KEEP_LABEL_BLANK: A blank label value is used. This works well when all you want is the copy graphic (obtained using CSS).
• Any other value: The value passed becomes the label.
setResultSetSize(switchTo)
The method setResultSetSize( ) is called to select the number of results returned by each searcher. The switchTo value is an enumeration that indicates either a small or a large number of results. Valid values for the argument are:
• GSearch.LARGE_RESULTSET: Request a large number of results (typically eight results).
• GSearch.SMALL resultset: Request a small number of results (typically four results).
setSearchCompleteCallback (object, method)
The method setsearchCompleteCallback( ) is used to inform the search control that the caller would like to be notified when the search completes. The callback method will be called for every search result returned (determined by the number of searchers attached). The object is an application-level object that defines the context in which the method will be called.
setSearchStartingCallback (object, method)
The method setsearchstartingCallback( ) is used to inform the search control that the user would like to be notified right before a search begins. The callback method will be called for every search result that starts (determined by the number of searchers
|
Th |
is document is created with |
trialversionofCHM2PDFPilot2.15.72. e | obj e ct t h at defines the context in which the method will be called. |
Post a comment