Advertisement
stuppid_bot

Untitled

Aug 31st, 2014
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Ajax() {}
  2. var ajax = new Ajax();
  3.  
  4. (function () {
  5.     'using strict';
  6.     var proto = Ajax.prototype;
  7.  
  8.     proto.getRequest = function (method, url, cb) {
  9.         var r = new XMLHttpRequest;
  10.         r.open(method, url);
  11.         r.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
  12.         r.onload = function () {
  13.             typeof cb == 'function' && cb(r.response, r.status, r);
  14.         };
  15.         return r;
  16.     };
  17.  
  18.     proto.get = function (url, cb) {
  19.         this.getRequest('GET', url, cb).send();
  20.     };
  21.  
  22.     proto.update = function (url, elem) {
  23.         this.get(url, function (data) {
  24.             val(elem, data);
  25.         });
  26.     };
  27.  
  28.     proto.post = function (url, params, cb) {
  29.         var r = this.getRequest('POST', url, cb), data = '', p;
  30.         r.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  31.         for (p in params) {
  32.             if (params.hasOwnProperty(p)) {
  33.                 if (data.length) data += '&';
  34.                 data += p + '=' + encodeURIComponent(params[p]);
  35.             }
  36.         }
  37.         r.send(data);
  38.     };
  39.  
  40.     proto.submit = function (frmElem, targetElem) {
  41.         frmElem = ge(frmElem);
  42.         var data = new FormData(frmElem);
  43.         this.getRequest('POST', frmElem.action, function (data) {
  44.             val(targetElem, data);
  45.         }).send(data);
  46.     };
  47. })();
  48.  
  49.  
  50. // ...
  51. function ge(el) {
  52.     return isElement(el) ? el : document.getElementById(el);
  53. }
  54.  
  55. function val(el, v) {
  56.     el = ge(el);
  57.     var i = /^(INPUT|TEXTAREA)$/.test(el.tagName) ? 'value': 'innerHTML';
  58.     return arguments.length > 1 ? el[i] = v : el[i];
  59. }
  60.  
  61. function extend(Child, Parent) {
  62.     var F = function () {};
  63.     F.prototype = Parent.prototype;
  64.     Child.prototype = new F();
  65.     Child.prototype.constructor = Child;
  66. }
  67.  
  68. function clone(o) {
  69.     if (o == null || typeof o != 'object')
  70.         return o;
  71.     var copy = o.constructor();
  72.     for (var p in o)
  73.         copy[p] = clone(o[p]);
  74.     return copy;
  75. }
  76.  
  77. function isElement(o) {
  78.     return o instanceof HTMLElement;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement