Skip to content

Instantly share code, notes, and snippets.

@jpcody
Created October 27, 2010 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpcody/649745 to your computer and use it in GitHub Desktop.
Save jpcody/649745 to your computer and use it in GitHub Desktop.
Pubsub Middle Ground
var search = {
getResults : function(){
$.getJSON(
'http://query.yahooapis.com/v1/public/yql?format=json&callback=',
{ q : getQuery(term) },
function(resp) {
if (!resp.query.results.result.length) { return; }
search.updateDOM(resp.query.results.result);
}
);
},
updateDOM : function(results){
var tmpl = '<li><p><a href="{{url}}">{{title}}</a></p></li>',
html = $.map(results, function(result) {
return tmpl
.replace('{{url}}', result.url)
.replace('{{title}}', result.title)
}).join('');
$('#results').html(html);
},
appendSearches : function(term){
$('#searches').append('<li>' + term + '</li>');
},
getQuery : function(term){
return 'select title,url from search.news where query="' + term + '"';
}
};
$(document).ready(function() {
$('#searchForm').submit(function(e) {
e.preventDefault();
var term = $.trim($(this).find('input[name="q"]').val());
if (!term) { return; }
search.getResults();
search.appendSearches();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment