1Cargo.utils = Cargo.utils || {}; 2 3/** 4 * Cargo.Core.CargoXHR wraps the native XMLHttpRequest with some extra features like retry. 5 * 6 * Kinda implements the jQuery (http://api.jquery.com/jquery.ajax/) ajax options. 7 * 8 * @author Aart Röst / Cargo Collective 9 * 10 */ 11 12Cargo.utils.CargoXHR = function(options){ 13 14 var self = this; 15 16 $.extend(this.options = {}, this.defaultOptions, options); 17 18 if(this.options.context === undefined){ 19 this.options.context = this; 20 } 21 22 if(this.options.url === undefined){ 23 throw 'no URL given, define "url" in the contructor\'s options object'; 24 } 25 26 this.retries = 0; 27 28 this.xhr = new XMLHttpRequest(); 29 30 this.xhr.addEventListener("abort", function(){ 31 self.options.abort.apply(self.options.context, arguments); 32 }, false); 33 34 this.xhr.addEventListener("error", function(){ 35 self.options.error.apply(self.options.context, arguments); 36 }, false); 37 38 this.xhr.addEventListener("load", function(){ 39 self.options.success.apply(self.options.context, arguments); 40 }, false); 41 42 this.xhr.addEventListener("loadend", function(){ 43 self.options.always.apply(self.options.context, arguments); 44 }, false); 45 46 this.xhr.upload.addEventListener("progress", function(e){ 47 self.options.progress.apply(self.options.context, arguments); 48 }, false); 49 50 this.send(); 51 52} 53 54Cargo.utils.CargoXHR.prototype.send = function(){ 55 56 this.xhr.open(this.options.type, this.options.url, this.options.async); 57 58 for(var key in this.options.headers){ 59 this.xhr.setRequestHeader(key, this.options.headers[key]); 60 } 61 62 this.xhr.responseType = this.options.dataType; 63 64 this.options.beforeSend.call(this, this.xhr); 65 66 this.xhr.send(this.options.data); 67 68} 69 70Cargo.utils.CargoXHR.prototype.retry = function(){ 71 72 if(this.retries++ < this.options.maxRetries){ 73 74 Cargo.utils.CargoXHR.prototype.send.apply(this, arguments); 75 76 } else { 77 78 this.abort(); 79 return false; 80 81 } 82 83} 84 85Cargo.utils.CargoXHR.prototype.abort = function(){ 86 87 if(typeof this.xhr === 'object'){ 88 this.xhr.abort(); 89 } 90 91} 92 93Cargo.utils.CargoXHR.prototype.defaultOptions = { 94 async: true, 95 beforeSend: function(XHR){ 96 97 }, 98 always: function(e){ 99 100 }, 101 success: function(e){ 102 103 }, 104 progress: function(e){ 105 106 }, 107 error: function(e) { 108 109 }, 110 abort: function(e) { 111 112 }, 113 type: 'GET', 114 url: undefined, 115 dataType: 'text', 116 headers: {}, 117 data: null, 118 context: undefined, 119 maxRetries: 5 120}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.