1 /**
  2  * @fileOverview io shortcut
  3  * @author yiminghe@gmail.com
  4  */
  5 KISSY.add("ajax", function (S, serializer, IO, XhrObject) {
  6     var undef = undefined;
  7 
  8     function get(url, data, callback, dataType, _t) {
  9         // data 参数可省略
 10         if (S.isFunction(data)) {
 11             dataType = callback;
 12             callback = data;
 13             data = undef;
 14         }
 15 
 16         return IO({
 17             type:_t || "get",
 18             url:url,
 19             data:data,
 20             success:callback,
 21             dataType:dataType
 22         });
 23     }
 24 
 25     // some shortcut
 26     S.mix(IO,
 27 
 28         /**
 29          * @lends IO
 30          */
 31         {
 32             XhrObject:XhrObject,
 33             /**
 34              * form serialization
 35              * @function
 36              * @param formElement {HTMLElement[]|HTMLElement|NodeList} form elements
 37              * @returns {String} serialized string represent form elements
 38              */
 39             serialize:serializer.serialize,
 40 
 41             /**
 42              * perform a get request
 43              * @function
 44              * @param {String} url request destination
 45              * @param {Object} [data] name-value object associated with this request
 46              * @param {Function()} callback <br/>
 47              * success callback when this request is done
 48              * with parameter <br/>
 49              * 1. data returned from this request with type specified by dataType <br/>
 50              * 2. status of this request with type String <br/>
 51              * 3. XhrObject of this request , for details {@link IO.XhrObject}
 52              * @param {String} [dataType] the type of data returns from this request
 53              * ("xml" or "json" or "text")
 54              * @returns {IO.XhrObject}
 55              */
 56             get:get,
 57 
 58             /**
 59              * preform a post request
 60              * @param {String} url request destination
 61              * @param {Object} [data] name-value object associated with this request
 62              * @param {Function()} callback <br/>
 63              * success callback when this request is done<br/>
 64              * with parameter<br/>
 65              * 1. data returned from this request with type specified by dataType<br/>
 66              * 2. status of this request with type String<br/>
 67              * 3. XhrObject of this request , for details {@link IO.XhrObject}
 68              * @param {String} [dataType] the type of data returns from this request
 69              * ("xml" or "json" or "text")
 70              * @returns {IO.XhrObject}
 71              */
 72             post:function (url, data, callback, dataType) {
 73                 if (S.isFunction(data)) {
 74                     dataType = callback;
 75                     callback = data;
 76                     data = undef;
 77                 }
 78                 return get(url, data, callback, dataType, "post");
 79             },
 80 
 81             /**
 82              * preform a jsonp request
 83              * @param {String} url request destination
 84              * @param {Object} [data] name-value object associated with this request
 85              * @param {Function()} callback
 86              *  <br/>success callback when this request is done<br/>
 87              * with parameter<br/>
 88              * 1. data returned from this request with type specified by dataType<br/>
 89              * 2. status of this request with type String<br/>
 90              * 3. XhrObject of this request , for details {@link IO.XhrObject}
 91              * @returns {IO.XhrObject}
 92              */
 93             jsonp:function (url, data, callback) {
 94                 if (S.isFunction(data)) {
 95                     callback = data;
 96                     data = undef;
 97                 }
 98                 return get(url, data, callback, "jsonp");
 99             },
100 
101             // 和 S.getScript 保持一致
102             // 更好的 getScript 可以用
103             /*
104              IO({
105              dataType:'script'
106              });
107              */
108             getScript:S.getScript,
109 
110             /**
111              * perform a get request to fetch json data from server
112              * @param {String} url request destination
113              * @param {Object} [data] name-value object associated with this request
114              * @param {Function()} callback  <br/>success callback when this request is done<br/>
115              * with parameter<br/>
116              * 1. data returned from this request with type JSON<br/>
117              * 2. status of this request with type String<br/>
118              * 3. XhrObject of this request , for details {@link IO.XhrObject}
119              * @returns {IO.XhrObject}
120              */
121             getJSON:function (url, data, callback) {
122                 if (S.isFunction(data)) {
123                     callback = data;
124                     data = undef;
125                 }
126                 return get(url, data, callback, "json");
127             },
128 
129             /**
130              * submit form without page refresh
131              * @param {String} url request destination
132              * @param {HTMLElement|NodeList} form element tobe submited
133              * @param {Object} [data] name-value object associated with this request
134              * @param {Function()} callback  <br/>success callback when this request is done<br/>
135              * with parameter<br/>
136              * 1. data returned from this request with type specified by dataType<br/>
137              * 2. status of this request with type String<br/>
138              * 3. XhrObject of this request , for details {@link IO.XhrObject}
139              * @param {String} [dataType] the type of data returns from this request
140              * ("xml" or "json" or "text")
141              * @returns {IO.XhrObject}
142              */
143             upload:function (url, form, data, callback, dataType) {
144                 if (S.isFunction(data)) {
145                     dataType = callback;
146                     callback = data;
147                     data = undef;
148                 }
149                 return IO({
150                     url:url,
151                     type:'post',
152                     dataType:dataType,
153                     form:form,
154                     data:data,
155                     success:callback
156                 });
157             }
158         });
159 
160     S.mix(S, {
161         Ajax:IO,
162         IO:IO,
163         ajax:IO,
164         io:IO,
165         jsonp:IO.jsonp
166     });
167 
168     return IO;
169 }, {
170     requires:[
171         "ajax/FormSerializer",
172         "ajax/base",
173         "ajax/XhrObject",
174         "ajax/XhrTransport",
175         "ajax/ScriptTransport",
176         "ajax/jsonp",
177         "ajax/form",
178         "ajax/IframeTransport"]
179 });