Implements JsonP support for the Autocompleter class.
| Mootools 1.1 | <Class.Extras>, <Element.Event>, <Element.Selectors>, <Element.Form>, <Element.Dimensions>, Fx.Style, Ajax, Json |
| Autocompleter | Autocompleter.js, Observer.js |
| CNET | jsonp.js |
Aaron Newton (aaron [dot] newton [at] cnet [dot] com)
| Autocompleter. JsonP.js | Implements JsonP support for the Autocompleter class. |
| Autocompleter. JsonP | Implements JsonP support for the Autocompleter class. |
| Properties | |
| queryResponse | Inherated 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 * |
Implements JsonP support for the Autocompleter class.
| el | (DOM element or id) element to observe. |
| url | (string) the url to query for values |
| options | (object) key/value set of 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). |
| filterResponse | Function, optional. Allows to override default filterResponse method |
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'
...
});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);
}
});| Properties | |
| queryResponse | Inherated classes have to extend this function and use this.parent(resp) |