Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created March 22, 2012 19:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkphp/2162676 to your computer and use it in GitHub Desktop.
Save thinkphp/2162676 to your computer and use it in GitHub Desktop.
YQLQuery
//Let's build the following contructor for YQL
function YQLQuery(query,callback,format,diagnostics) {
this.query = query;
this.format = format || 'json';
this.diagnostics = diagnostics || false;
this.callback = callback || function(){};
}
YQLQuery.prototype.fetch = function() {
if(!this.query || !this.callback) {
console.log("Fetch error: missing parameters!");
return;
}
var scriptEl = document.createElement('script'),
endpoint = 'http://query.yahooapis.com/v1/public/yql?q=',
env = '&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys',
encodedURL = encodeURIComponent(this.query),
format = this.format,
id = "yql" + (+new Date()),
that = this,
src = endpoint + encodedURL + '&format='+ format + '&callback=YQLQuery.' + id + '&diagnostics=' + this.diagnostics + env;
YQLQuery[id]= function(data) {
if(window.console) {console.log(data);}
that.callback(data);
delete YQLQuery[id];
document.body.removeChild(scriptEl);
}
scriptEl.src = src;
document.body.appendChild(scriptEl);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment