Autocompleter. JsonP.js

Implements JsonP support for the Autocompleter class.

Dependencies

Mootools 1.1<Class.Extras>, <Element.Event>, <Element.Selectors>, <Element.Form>, <Element.Dimensions>, Fx.Style, Ajax, Json
AutocompleterAutocompleter.js, Observer.js
CNETjsonp.js

Author

Aaron Newton (aaron [dot] newton [at] cnet [dot] com)

Summary
Autocompleter. JsonP.jsImplements JsonP support for the Autocompleter class.
Autocompleter. JsonPImplements JsonP support for the Autocompleter class.
Properties
queryResponseInherated classes have to extend this function and use this.parent(resp)
Change Log$Source: /cvs/main/flatfile/html/rb/js/global/cnet.global.framework/common/3rdParty/Autocomplete/Autocompleter.JsonP.js,v $ $Log: Autocompleter.JsonP.js,v $ Revision 1.1 2007/06/12 20:26:52 newtona * empty log message *

Autocompleter. JsonP

Implements JsonP support for the Autocompleter class.

Arguments

el(DOM element or id) element to observe.
url(string) the url to query for values
options(object) key/value set of options.

Options

postVar(string) the key to which the user’s entry is mapped - passed to the server as postVar=userEntry (see example below)
jsonpOptions(object) options passed along to the JsonP class.
onRequest(callback function) fired when the request is sent
onComplete(callback function) fired when the request is complete
minLength(integer) Overrides minLength (defaults to 1).
filterResponseFunction, optional.  Allows to override default filterResponse method

Example

Let’s say the user is typing into an input to search for ipods and you need to take what they’ve typed (“ipo” so far) and send it to a server to get back filtered results like so:

http://server.com/handler.jsp?query=ipo

Then the postVar option would be “query” so that the user’s input is mapped to this key in the query string.

var myCompleter = new Autocomplete.JsonP($('myinput'), 'http://server.com/handler.jsp', {
postVar: 'query'
...
});

You’re not really done though, because you need to handle the results that come back using the functionality in the base Autocompleter class.  Here’s an example that will work with the cnet API

new Autocompleter.JsonP($('jsonp'), 'http://api.cnet.com/restApi/v1.0/techProductSearch',
{
jsonpOptions: {
//this data gets added to the query string using JsonP's options
data: {
viewType: 'json',
partKey: '19926949750937665684988687810562', //this is my code, user your own!
iod:'none',
start:0,
results:10
}
},
//require at least a key stroke from the user
minLength: 1,
//this function filters the results based on the input
filterResponse: function(resp) {
//test it
if(!choices || choices.length == 0) return [];
//filter it and return it
var regex = new RegExp('^' + (this.queryValue || '').escapeRegExp(), 'i');
return choices.filter(function(choice){
return (regex.test(choice.Name.$) || regex.test(choice['@id']));
});
},
useSelection: false,
//because the data returned has a unique structure, we must manage the parsing ourselves
filterResponse: function(resp) {
try {
//this structure is unique to the CNET API
choices = resp.CNETResponse.TechProducts.TechProduct;
//test it
if(!choices || choices.length == 0) return [];
//filter it and return it
return choices.filter(function(choice){
return (choice.Name.$.test(this.getQueryValue(), 'i') || choice['@id'].test(this.getQueryValue()), 'i');
}.bind(this));
} catch(e){'filterResponse error: ', dbug.log(e)}
},
injectChoice: function(choice) {
//again, the structure of these items is unique to the CNET API
if(! choice.Name.$)return;
var el = new Element('li')
.setHTML(this.markQueryValue(choice.Name.$))
.adopt(new Element('span', {'class': 'example-info'}).setHTML(this.markQueryValue(choice['@id'])));
el.inputValue = choice.Name.$+' ('+choice['@id']+')';
this.addChoiceEvents(el).injectInside(this.choices);
}
});
Summary
Properties
queryResponseInherated classes have to extend this function and use this.parent(resp)

Properties

queryResponse

Inherated classes have to extend this function and use this.parent(resp)

Arguments

resp(String) the response from the ajax query.

Change Log

$Source: /cvs/main/flatfile/html/rb/js/global/cnet.global.framework/common/3rdParty/Autocomplete/Autocompleter.JsonP.js,v $ $Log: Autocompleter.JsonP.js,v $ Revision 1.1 2007/06/12 20:26:52 newtona * empty log message *

Creates a Json request using a script tag include and handles the callbacks for you.
All functions are part of the Autocompleter namespace.
Base class for the Mootools Effects (Moo.Fx) library.
The Style effect; Extends Fx.Base, inherits all its properties.
An Ajax class, For all your asynchronous needs.
Simple Json parser and Stringyfier, See: http://www.json.org/
3rd party script for managing autocomplete functionality.
Observes formelements for changes; part of the <Autocomplete> 3rd party package.
Creates a Json request using a script tag include and handles the callbacks for you.