1 /**
  2  * @fileOverview jsonp transport based on script transport
  3  * @author  yiminghe@gmail.com
  4  */
  5 KISSY.add("ajax/jsonp", function (S, io) {
  6     var win = S.Env.host;
  7     io.setupConfig({
  8         jsonp:"callback",
  9         jsonpCallback:function () {
 10             // 不使用 now() ,极端情况下可能重复
 11             return S.guid("jsonp");
 12         }
 13     });
 14 
 15     io.on("start", function (e) {
 16         var xhrObject = e.xhr,
 17             c = xhrObject.config;
 18         if (c.dataType[0] == "jsonp") {
 19             var response,
 20                 cJsonpCallback = c.jsonpCallback,
 21                 jsonpCallback = S.isFunction(cJsonpCallback) ?
 22                     cJsonpCallback() :
 23                     cJsonpCallback,
 24                 previous = win[ jsonpCallback ];
 25 
 26             c.url += ( /\?/.test(c.url) ? "&" : "?" ) + c.jsonp + "=" + jsonpCallback;
 27 
 28             // build temporary JSONP function
 29             win[jsonpCallback] = function (r) {
 30                 // 使用数组,区别:故意调用了 jsonpCallback(undefined) 与 根本没有调用
 31                 // jsonp 返回了数组
 32                 if (arguments.length > 1) {
 33                     r = S.makeArray(arguments);
 34                 }
 35                 response = [r];
 36             };
 37 
 38             // cleanup whether success or failure
 39             xhrObject.fin(function () {
 40                 win[ jsonpCallback ] = previous;
 41                 if (previous === undefined) {
 42                     try {
 43                         delete win[ jsonpCallback ];
 44                     } catch (e) {
 45                         //S.log("delete window variable error : ");
 46                         //S.log(e);
 47                     }
 48                 } else if (response) {
 49                     // after io success handler called
 50                     // then call original existed jsonpcallback
 51                     previous(response[0]);
 52                 }
 53             });
 54 
 55             xhrObject.converters = xhrObject.converters || {};
 56             xhrObject.converters.script = xhrObject.converters.script || {};
 57 
 58             // script -> jsonp ,jsonp need to see json not as script
 59             // if ie onload a 404 file or all browsers onload an invalid script
 60             // 404/invalid will be caught here
 61             // because response is undefined( jsonp callback is never called)
 62             // error throwed will be caught in conversion step
 63             // and KISSY will notify user by error callback
 64             xhrObject.converters.script.json = function () {
 65                 if (!response) {
 66                     S.error(" not call jsonpCallback : " + jsonpCallback)
 67                 }
 68                 return response[0];
 69             };
 70 
 71             c.dataType.length = 2;
 72             // 利用 script transport 发送 script 请求
 73             c.dataType[0] = 'script';
 74             c.dataType[1] = 'json';
 75         }
 76     });
 77 
 78     return io;
 79 }, {
 80     requires:['./base']
 81 });
 82