Let's jump straight into the coding with no further ado. We'll use the page and code of our previous example for this one. Out of the code, we'll keep this section, which we have already discussed.
var YQL_QUERY = 'select * from search.suggest where query="{query}"', YQL_URL_ARGS = "format=json&diagnostics=false&q=", YQL_SERVICE = "http://query.yahooapis.com/v1/public/yql?", FORMAT = "Your selection was: <b>{0}<\/b>"; var myDS = new DS(YQL_SERVICE,{ dataType: DS.TYPE_SCRIPTNODE, responseType: DS.TYPE_JSARRAY, responseSchema: { fields: ["suggestion"] } }); myDS.doBeforeParseData = function ( oRequest , oFullResponse) { if (parseInt(oFullResponse.query.count,10)) { return oFullResponse.query.results.Result; } else { return []; } };
The only difference with our previous version is that in .doBeforeParseData()
we've been a little more careful because there might not be any suggestions offered so we return an empty array. With the DataSource defined...