Skip to content

Instantly share code, notes, and snippets.

View thinkphp's full-sized avatar

Adrian Statescu thinkphp

View GitHub Profile
@thinkphp
thinkphp / gist:2162717
Created March 22, 2012 19:39
YQLQuery minified
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);};
@thinkphp
thinkphp / gist:2162676
Created March 22, 2012 19:38
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() {
@thinkphp
thinkphp / gist:2148831
Created March 21, 2012 15:51
jsonp v2 minified
function Jsonp(url,callback){this.url=url;this.callback=callback;}Jsonp.prototype.fetch=function(){var id=new Date().getTime(),fn="callback_"+id,url=this.url.replace("=?","=Jsonp."+fn),that=this,s=document.createElement("script");s.type="text/javascript";Jsonp[fn]=this.evalJSON(this.callback,that);s.src=url;document.getElementsByTagName("head")[0].appendChild(s);this.s=s;this.fn=fn;};Jsonp.prototype.evalJSON=function(callback,that){return function(data){var validjson=false;if(typeof data=="string"){try{validjson=JSON.parse(data);}catch(e){}}else{if(typeof data=="object"){validjson=data;}else{validjson=JSON.parse(JSON.stringify(data));console.log("response data was not a JSON string");}}if(validjson){callback(validjson);delete Jsonp[that.fn];document.getElementsByTagName("head")[0].removeChild(that.s);}else{console.log("JSONP call returned invalid JSON or empty JSON");}};};
@thinkphp
thinkphp / gist:2148789
Created March 21, 2012 15:48
jsonp v2
function Jsonp(url, callback) {
this.url = url;
this.callback = callback
}
Jsonp.prototype.fetch = function() {
var id = new Date().getTime(),
fn = "callback_" + id,
@thinkphp
thinkphp / gist:2146639
Created March 21, 2012 12:39
jsonp2 usage
var tpl = "<li><a href='http://twitter.com/{from_user}'>{from_user}</a> {text}<span>{created_at}</span></li>",
urltwitter = 'http://search.twitter.com/search.json?q=mootools&rpp=5&callback=?';
new Jsonp(urltwitter, function(data){
var result = data.results,
out = '<ul>'
@thinkphp
thinkphp / gist:2145914
Created March 21, 2012 10:04
jsonp usage
var urltwitter = 'http://search.twitter.com/search.json?q=mootools&rpp=5&callback=?'
jsonp.fetch(urltwitter, function(data){
var result = data.results,
out = '<ul>'
for(var i in result) {
@thinkphp
thinkphp / gist:2145911
Created March 21, 2012 10:03
jsonp minified
var jsonp={counter:0,fetch:function(url,callback){var fn="jsoncallback_"+this.counter++;window[fn]=this.evalJsonp(callback);url=url.replace("=?","="+fn);var s=document.createElement("script");s.type="text/javascript";s.src=url;document.getElementsByTagName("head")[0].appendChild(s);},evalJsonp:function(callback){return function(data){var validjson=false;if(typeof data=="string"){try{validjson=JSON.parse(data);}catch(e){}}else{if(typeof data=="object"){validjson=data;}else{validjson=JSON.parse(JSON.stringify(data));console.log("response data was not a JSON string");}}if(validjson){callback(validjson);}else{console.log("JSONP call returned invalid JSON or empty JSON");}};}};
var jsonp = {
counter: 0,
fetch: function(url, callback) {
var fn = "jsoncallback_" + this.counter++;
window[fn] = this.evalJsonp(callback);
@thinkphp
thinkphp / gist:1862882
Created February 19, 2012 09:54
cos(x) in PHP
<?php
/*
* @thinkphp
*
* Approximate the function sin with series Taylor!
* cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + ....(-1)^n*x^(2n)/(2n)!;
*
*/
function fact($n) {
@thinkphp
thinkphp / gist:1862877
Created February 19, 2012 09:54
sin(x) in PHP
/*
* Twitter @thinkphp
*
* Approximate the function sin with series Taylor!
* sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ....(-1)^n+1*x^(2n+1)/(2n+1)!;
*
*/
function fact($n) {