@GoogleSuggest()
During AD111, I mentioned that SSJS supports custom @Functions, but advised against defining your own. My primary reason for advising against it is the possibility that IBM may, in a future version of Domino, implement their own version of whatever custom function you decided to define. Back when version 6 was released, I saw far too many apps break because developers had defined their own LotusScript Replace() function in previous versions.
On the other hand, XPages are capable of so much by comparison to traditional Notes apps that it's inevitable that we'll occasionally identify functionality we'd like to encapsulate into a function that IBM probably will never make a part of the core product. So here's an example of a handy custom @Function that I doubt will ever be native to the XPages implementation of SSJS:
importPackage(javax.xml.parsers);
var @GoogleSuggest = function(partial:string) {
var resultKey = "googleSuggest-" + partial;
if (!(applicationScope.containsKey(resultKey))) {
var results = [];
var baseUrl:string = "http://google.com/complete/search?output=toolbar&q=";
var factory = DocumentBuilderFactory.newInstance();
var parser = factory.newDocumentBuilder();
var domResult:DOMDocument = parser.parse(baseUrl + partial);
var resultNodes:DOMNodeList = domResult.getElementsByTagName("suggestion");
for (var nodeIndex:int = 0; nodeIndex < resultNodes.getLength(); nodeIndex++) {
var resultNode:DOMElement = resultNodes.item(nodeIndex);
results.push(resultNode.getAttribute("data"));
}
results.sort();
applicationScope.put(resultKey, results);
}
return applicationScope.get(resultKey);
};
If you add that to a SSJS script library, then add the library as a resource on an XPage, then you can easily set the typeahead for any edit box to be the Google Suggest results for whatever value the user types:
<xp:inputText id="search">
<xp:typeAhead mode="partial" minChars="3" var="partial"
valueList="#{javascript:@GoogleSuggest(partial)}">
</xp:typeAhead>
</xp:inputText>

Comments
Couldn't you prevent this simply by giving the function a name IBM would never use? For example, instead of "Replace()", call the function "CustomReplace()"?
Posted by Timothy Briley At 11:33:08 AM On 02/22/2010 | - Website - |
In SSJS, defining a custom @Function that may later be natively implemented by IBM carries less danger with it due to the nature of JavaScript (the custom definition would simply override the behavior of the native definition, so any code calling it should not break) but in most cases would be subject to similar performance implications.
Posted by Tim Tripcony At 11:50:11 AM On 02/22/2010 | - Website - |
Realy nice code ;)
But somethings wrong in my implementation.
In the Typeahead respone i get a standard xpages error.
SSJS is the same as in your example.
Could you help me?
Error while executing JavaScript computed expression
Script interpreter error, line=9, col=44: Error calling method 'parse(string)' on java class 'org.apache.xerces.jaxp.DocumentBuilderImpl'
Connection refused: connect
9: var domResult:DOMDocument = parser.parse(baseUrl + partial);
Posted by Wueste At 09:20:14 AM On 02/23/2010 | - Website - |
Posted by Tim Tripcony At 09:50:04 AM On 02/23/2010 | - Website - |
Posted by Theo Heselmans At 11:01:57 AM On 02/23/2010 | - Website - |
var com = com || {};
com.timtripcony = {};
com.timtripcony.GoogleSuggest = function(....) { ... }
and then call it with
com.trimtripcony.GoogleSuggest(...);
Yes I know... what if your domain name changes because of an acquisition or something. Well... there are +'es and -'es to everything
Posted by John Foldager At 06:45:02 AM On 03/16/2010 | - Website - |
Posted by Tim Tripcony At 09:21:40 AM On 03/16/2010 | - Website - |