Skip to content

Instantly share code, notes, and snippets.

@joerussbowman
Created January 15, 2011 23:54
Show Gist options
  • Save joerussbowman/781383 to your computer and use it in GitHub Desktop.
Save joerussbowman/781383 to your computer and use it in GitHub Desktop.
Example yui3 autocomplete using yql
<input type="text" name="q" id="q" />
<script type="text/javascript">
YUI().use("autocomplete", "autocomplete-highlighters", "datasource-get", function (Y) {
var acDS = new Y.DataSource.Get({
source: "http://sugg.search.yahoo.com/gossip-us-fp/?nresults=10&queryfirst=2&output=json&version=&command=",
});
Y.one('#q').plug(Y.Plugin.AutoComplete, {
maxResults: 10,
resultHighlighter: 'phraseMatch',
requestTemplate: "{query}",
resultListLocator: 'gossip.results',
resultTextLocator: 'key',
source: acDS
});
});
</script>
@rgrove
Copy link

rgrove commented Jan 16, 2011

The problem here is that since you're embedding the query in a URL which is itself embedded in another URL for the sake of the YQL query, you need to double-escape the query. AutoComplete only single-escapes it, since it has no way of knowing that you're embedding a URL in another URL.

Your best bet here is probably either to wrap the sugg.search API in a custom YQL table and then just use that table (so you won't need to double-escape), or use DataSource to talk to YQL, which will give you full control over the escaping.

@joerussbowman
Copy link
Author

hmm.. maybe I'll just stick with search.suggest for now and figure out something else later. Better than getting ip blocked or annoying anyone. I'm just moving unscatter.com to use search.web and search.news and then use blekko for the more vertical searches like blogs and reviews.

@rgrove
Copy link

rgrove commented Jan 16, 2011

Actually, after doing some digging, I edited my comment to remove the bit about possibly getting IP blocked. That shouldn't be a problem.

But yeah, anything that doesn't require you to embed a URL within the YQL query would be a better bet. I'll consider adding an option to customize escaping behavior for queries used with YQL sources (you can already customize it for other source types).

@joerussbowman
Copy link
Author

Well I've been meaning to figure out how to use datasource better, so I'll go straight with datasource to the sugg.search endpoint. The extra hop should speed it up a little bit, though honestly it was plenty fast using YQL. Thanks for digging around on that, nice to know I can use that endpoint. :)

@rgrove
Copy link

rgrove commented Jan 16, 2011

To clarify: I'm not officially saying you're allowed to use sugg.search, I'm just saying you probably won't get blocked. ;)

Here's an example showing how to use AutoComplete+DataSource+YQL: http://developer.yahoo.com/yui/3/examples/autocomplete/ac_datasource.html

@joerussbowman
Copy link
Author

Fair enough. I'm actually putting the query client side in the browser, so if anyone complains I'll just pull it down quickly. It's so raw right now I only have 1 person who uses it beside me. I was thinking I'd just skip YQL altogether and do a datasource direct to sugg.search.

@joerussbowman
Copy link
Author

If you get a sec, can you take a look at the new version of this gist and tell me why it's not working? Based off of everything I've read it should work, but it's not creating the list at all.

@rgrove
Copy link

rgrove commented Jan 16, 2011

DataSource.Get requires a response wrapped in a JSONP callback. The sugg.search API doesn't let you specify a custom JSONP callback, so this can't work. You'll have to go through YQL to get past the same domain restriction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment