Some examples of using the OpenSearchStore are available in the /ibm_opensearch/data/demos folder. These demos reference OpenSearch description documents which must be fetched and edited to send the requests through a local proxy, as cross-domain requests are not possible in current browsers.
This example is constructed as if the OpenSearch endpoint is hosted on the same server as this package, therefore requiring no added complexity of setting up a proxy. See the following sample OpenSearch Description Document with only the required elements included:
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"> <ShortName>Sample Search Engine</ShortName> <Description>Searches each element at example.com</Description> <Url type="application/atom+xml" template="http://example.com/search/{searchTerms}" /> </OpenSearchDescription>
The description document is hosted at a URL such as http://example.com/osd.xml, with a mime-type of application/opensearchdescription+xml. A data store in the page "http://example.com/index.html" can now be instantiated with the following markup:
<script src="dojo/dojo.js"></script> <script> dojo.require("ibm_opensearch.data.OpenSearchStore"); dojo.require("dojo.parser"); </script> <div dojoType="ibm_opensearch.data.OpenSearchStore" url="osd.xml" jsId="openSearchStore"> </div>
After the page is loaded and parsed, the store is available by referencing the variable, openSearchStore. You can use the store to fetch results based on keywords:
<table> <tbody id="searchResults"> </tbody> </table> <script> dojo.addOnLoad(function(){ function onComplete(items, request){ if(items.length > 0){ var tbody = dojo.byId("searchResults"); var test; var tr, td; for(var i=0; i<items.length; i++){ td = dojo.doc.createElement("td"); td.innerHTML = openSearchStore.getValue(items[i], "content"); tr = dojo.doc.createElement("tr"); tr.appendChild(td); tbody.appendChild(tr); } } } //What to do if a search fails function onError(error, request){ statusWidget.setValue("PROCESSING ERROR."); } var request = { query: {searchTerms: "atom"}, onComplete: onComplete, onError: onError }; openSearchStore.fetch(request); }); </script>
The previous example forms a request object, setting the searchTerms parameter to atom, which indicates the keyword to search for on the server. Next, the fetch(request) is called on the store, which passes the results to the onComplete function. This function iterates over each result, gets the content attribute, the only available attribute on an OpenSearchStore item, and puts it into a table cell that is displayed on the page.
More detailed usage examples, including using a text box to input the search terms are shown on the demo pages.