/* Script: jsonp.js Creates a Json request using a script tag include and handles the callbacks for you. Dependencies: Mootools - , , , , , , Author: Aaron Newton Class: JsonP Creates a Json request using a script tag include and handles the callbacks for you. Arguments: url - the url to get the json data options - an object with key/value options Options: onComplete - (optional) function to execute when the data returns; it will be passed the data callBackKey - (string) the key in the url that the server uses to wrap the Json results. So, for example, if you used "callBackKey: 'callback'" then the server is expecting something like http://..../?q=search+term&callback=myFunction defaults to "callback". This must be defined correctly. queryString - (string, optional) additional query string values to append to the url data - (object, optional) additional key/value data to append to the url Example: (start code) new JsonP('http://api.cnet.com/restApi/v1.0/techProductSearch', { data: { partTag: 'mtvo', iod: 'hlPrice', iewType: 'json', results: '100', query: 'ipod' }, onComplete: myFunction.bind(someObject) }).request(); (end) The above example would generate this url: (start code) http://api.cnet.com/restApi/v1.0/techProductSearch?partTag=mtvo&iod=hlPrice&viewType=json&results=100&query=ipod&callback=JsonP.requestors[0].handleResults& (end) It would embed this script tag (in the head of the document) and, when it loaded, execute the "myFunction" callback defined. */ var JsonP = new Class({ options: { onComplete: Class.empty, callBackKey: "callback", queryString: "", data: {} }, initialize: function(url, options){ this.setOptions(options); this.url = this.makeUrl(url); this.fired = false; }, /* Property: request Executes the Json request. */ request: function(){ var dl = (window.ie)?50:0; //for some reason, IE needs a moment here... (function(){ this.script = new Asset.javascript(this.url); this.fired = true; }.bind(this)).delay(dl); }, makeUrl: function(url){ var index = JsonP.requestors.push(this) - 1; var separator = (url.test('\\?'))?'&':'?'; var jurl = url + separator + this.options.callBackKey + "=JsonP.requestors[" + index+"].handleResults"; if(this.options.queryString) jurl += "&"+this.options.queryString; jurl += "&"+Object.toQueryString(this.options.data); return jurl; }, handleResults: function(data){ this.fireEvent('onComplete', data); this.script.remove(); } }); JsonP.requestors = []; JsonP.implement(new Options); JsonP.implement(new Events);