Upgrade to ExtJS 3.2.0 - Released 03/30/2010
[extjs.git] / docs / source / jsonp.html
1 <html>
2 <head>
3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    
4   <title>The source code</title>
5     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 </head>
8 <body  onload="prettyPrint();">
9     <pre class="prettyprint lang-js">/*!
10  * Ext JS Library 3.2.0
11  * Copyright(c) 2006-2010 Ext JS, Inc.
12  * licensing@extjs.com
13  * http://www.extjs.com/license
14  */
15 Ext.ns('Ext.ux');
16
17 Ext.ux.JSONP = (function(){
18     var _queue = [],
19         _current = null,
20         _nextRequest = function() {
21             _current = null;
22             if(_queue.length) {
23                 _current = _queue.shift();
24                         _current.script.src = _current.url + '?' + _current.params;
25                         document.getElementsByTagName('head')[0].appendChild(_current.script);
26             }
27         };
28
29     return {
30         request: function(url, o) {
31             if(!url) {
32                 return;
33             }
34             var me = this;
35
36             o.params = o.params || {};
37             if(o.callbackKey) {
38                 o.params[o.callbackKey] = 'Ext.ux.JSONP.callback';
39             }
40             var params = Ext.urlEncode(o.params);
41
42             var script = document.createElement('script');
43                         script.type = 'text/javascript';
44
45             if(o.isRawJSON) {
46                 if(Ext.isIE) {
47                     Ext.fly(script).on('readystatechange', function() {
48                         if(script.readyState == 'complete') {
49                             var data = script.innerHTML;
50                             if(data.length) {
51                                 me.callback(Ext.decode(data));
52                             }
53                         }
54                     });
55                 }
56                 else {
57                      Ext.fly(script).on('load', function() {
58                         var data = script.innerHTML;
59                         if(data.length) {
60                             me.callback(Ext.decode(data));
61                         }
62                     });
63                 }
64             }
65
66             _queue.push({
67                 url: url,
68                 script: script,
69                 callback: o.callback || function(){},
70                 scope: o.scope || window,
71                 params: params || null
72             });
73
74             if(!_current) {
75                 _nextRequest();
76             }
77         },
78
79         callback: function(json) {
80             _current.callback.apply(_current.scope, [json]);
81             Ext.fly(_current.script).removeAllListeners();
82             document.getElementsByTagName('head')[0].removeChild(_current.script);
83             _nextRequest();
84         }
85     }
86 })();</pre>    
87 </body>
88 </html>