Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created February 28, 2013 16:52
Show Gist options
  • Select an option

  • Save thinkphp/5058155 to your computer and use it in GitHub Desktop.

Select an option

Save thinkphp/5058155 to your computer and use it in GitHub Desktop.
Google Search using cui.router and script loader
use.path("../src/core")
use(["cui","dom","event","jsonp","router"], function(){
//dom is available and ready to use
var dom = cui.dom,
//event is available and ready to use
event = cui.event,
//jsonp is available and ready to use
jsonp = cui.jsonp,
//router is ready and ready to use
app = cui.router;
var googlesearch = function(){
/* defines the vars*/
//for google form
var f,
//for div container
resultscontainer,
//for button
send,
//var for textfield
q;
//@public init function
function init() {
//get the form element
f = dom.select("#googleform"),
//get the button element
send = dom.select('#send'),
//get the input text element
q = dom.select('#q'),
//get the div result
resultscontainer = dom.select('#results');
//if all exist then attach the handler for event submit
if(f && resultscontainer && send && q) {
//bind the handler to the event submit
dom.select('#googleform').Submit(function( event ){
googlesearch.get( event )
})
}
}
//handler for submit event
function get( evt ) {
//stop propagation
event.stopPropagation( evt )
//change attribute with loading
dom.select('#send').attr('value','Loading...')
//change window location hash
window.location = "#/search/" + q.value;
};
return {init: init, get: get};
}();
//call init function
googlesearch.init();
// define a var f that holds the handler function
// and fires when location hash is changed.
var f;
//router defined
app.get("/search/:what", f = function( data ){
//assemble the URL for REST GET request
var url = 'http://ajax.googleapis.com/ajax/services/search/web?gl=en'+
'&userip=&'+
'hl=en&'+
'v=1.0&'+
'start=0&'+
'rsz=8&'+
'q='+data.what;
//change the attr value for the textfield input
dom.select("#q").value = data.what;
//change attribute of the element with id "q" textfield input
dom.select("#q").attr("value", data.what);
//make a JSONP request
jsonp(url, function( o ){
//grab the data from server
if(window.console) console.log( o )
//change the attribute 'value' for the button search
dom.select('#send').attr('value','Go!')
//declare a val with '' value
var output = '';
//if resposenstatus is ok then
if(o.responseStatus == 200) {
//define a var
var out = '',
//grab the results from object received
results = o.responseData.results;
//if the results are not undefined and are object then
if(undefined !== results && typeof results === 'object') {
//get the length of the results
var all = results.length;
//append a h3
out += '<h3>Web Results</h3><ol>';
//loop through all the results and assemble the result
for(var i=0;i<all;i++) {
out += '<li><h4><a href="' + results[i].url + '">' + results[i].title + '</a></h4>' +
'<p>' + results[i].content + '<span>(' + results[i].url + ')</span></p>';
}
out += '</ol>';
}
//final output
output = '<h2>Results</h2><div>' + out + '</div>';
//otherwise error
} else {
out = '<div class="error"><h2>Error</h2>' +
'<p>'+ o.responseDetails +'</p>' +
'</div>';
}
//append the container result into the div reserved for results
dom.select("#results").html( out )
})
})
//run the router
app.run();
if(window.location.hash.indexOf("/search") != -1) {
var _val = location.hash.split("/")[2]
dom.select("#q").attr("value", _val);
f({what: _val});
}
});//enduse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment