|
1 | 1 | var Ao = (function () { |
2 | 2 | // Helpers |
| 3 | + function doNothing() { } |
| 4 | + |
3 | 5 | function doOne(callback) { |
4 | 6 | callback.call(this, this.e, 0); |
5 | 7 | return this; |
|
36 | 38 | this._data = {}; |
37 | 39 | if (typeof elemOrId === 'undefined') { |
38 | 40 | single(this, empty); |
39 | | - } |
40 | | - else if (typeof elemOrId === 'string') { |
| 41 | + } else if (typeof elemOrId === 'string') { |
41 | 42 | single(this, ao.get(elemOrId)); |
42 | | - } |
43 | | - else { |
| 43 | + } else { |
44 | 44 | if (isArrayLike(elemOrId)) { |
45 | 45 | if (elemOrId.length === 1) { |
46 | 46 | single(this, elemOrId[0]); |
47 | | - } |
48 | | - else { |
| 47 | + } else { |
49 | 48 | this.e = elemOrId; |
50 | 49 | this._do = doAll; |
51 | 50 | } |
52 | | - } |
53 | | - else { |
| 51 | + } else { |
54 | 52 | single(this, elemOrId); |
55 | 53 | } |
56 | 54 | } |
|
294 | 292 |
|
295 | 293 | ao.isArrayLike = isArrayLike; |
296 | 294 |
|
| 295 | + ao.merge = function (to, from) { |
| 296 | + for (var propertyName in from) { |
| 297 | + if (from.hasOwnProperty(propertyName)) { |
| 298 | + to[propertyName] = from[propertyName]; |
| 299 | + } |
| 300 | + } |
| 301 | + return to; |
| 302 | + }; |
| 303 | + |
| 304 | + ao.serialize = function (obj) { |
| 305 | + var str = []; |
| 306 | + for (var propertyName in obj) { |
| 307 | + if (obj.hasOwnProperty(propertyName)) { |
| 308 | + str.push(encodeURIComponent(propertyName) + '=' + encodeURIComponent(obj[propertyName])); |
| 309 | + } |
| 310 | + } |
| 311 | + return str.join('&'); |
| 312 | + }; |
| 313 | + |
| 314 | + // Ajax |
| 315 | + function getXhr() { |
| 316 | + return window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); |
| 317 | + } |
| 318 | + |
| 319 | + var AjaxOpts = function () { }; |
| 320 | + AjaxOpts.prototype = { |
| 321 | + type: 'GET', |
| 322 | + async: true |
| 323 | + }; |
| 324 | + AjaxOpts.prototype.onFail = doNothing; |
| 325 | + AjaxOpts.prototype.onSuccess = doNothing; |
| 326 | + |
| 327 | + var done = 4; |
| 328 | + |
| 329 | + ao.ajax = function (userOpts) { |
| 330 | + var opts = ao.merge(new AjaxOpts(), userOpts); |
| 331 | + var xhr = getXhr(); |
| 332 | + xhr.open(opts.type, opts.url, opts.async); |
| 333 | + xhr.onreadystatechange = function () { |
| 334 | + if (xhr.readyState === done) { |
| 335 | + var response = Boolean(xhr.responseText) ? JSON.parse(xhr.responseText) : {}; |
| 336 | + response.statusCode = xhr.status; |
| 337 | + if (opts.hasOwnProperty('state')) { |
| 338 | + response.state = opts.state; |
| 339 | + } |
| 340 | + if (xhr.status >= 400) { |
| 341 | + opts.onFail(response); |
| 342 | + } else { |
| 343 | + try { |
| 344 | + opts.onSuccess(response); |
| 345 | + } catch (err) { |
| 346 | + response.error = err; |
| 347 | + opts.onFail(response); |
| 348 | + } |
| 349 | + } |
| 350 | + } |
| 351 | + }; |
| 352 | + if (opts.hasOwnProperty('data') === false) { |
| 353 | + xhr.send(); |
| 354 | + return; |
| 355 | + } |
| 356 | + var dataType, data; |
| 357 | + if (opts.hasOwnProperty('isJson') && opts.isJson === true) { |
| 358 | + dataType = 'json'; |
| 359 | + data = JSON.stringify(opts.data); |
| 360 | + } else { |
| 361 | + dataType = 'x-www-form-urlencoded'; |
| 362 | + data = ao.serialize(opts.data); |
| 363 | + } |
| 364 | + xhr.setRequestHeader('Content-Type', 'application/' + dataType); |
| 365 | + xhr.send(data); |
| 366 | + }; |
| 367 | + |
297 | 368 | ao.form = function (form) { |
298 | 369 | if (typeof form === 'undefined') { |
299 | 370 | form = document.forms[0]; |
|
0 commit comments