1Namespace.register('mira.data'); 2 3(function(ns){ 4 5 var Request = function(url,method) { 6 7 // set properties 8 this._url = url; 9 this._method = method || 'GET'; 10 11 // not loading right now 12 this._loading = false; 13 14 // set xhr object 15 if (!window.XMLHttpRequest) { 16 try { 17 this._xmlHttp = new ActiveXObject('Microsoft.XMLHTTP'); 18 } 19 catch(e) { 20 // no xmlhttp support, do nothing 21 return; 22 } 23 } 24 else { 25 this._xmlHttp = new XMLHttpRequest(); 26 } 27 28 // listen to state change on xhr 29 var self = this; 30 this._xmlHttp.onreadystatechange = function(e) { 31 self.onReadyStateChange(e); 32 }; 33 }; 34 35 var p = Request.prototype; 36 37 p.send = function(post) { 38 39 // abort if already loading 40 if (this._loading) { 41 this.abort(); 42 } 43 44 // prevent request caching by adding timestamp to url 45 var url = this._url + (this._url.indexOf('?') != -1 ? '&' : '?') + 'ts=' + new Date().getTime(); 46 47 try { 48 // try to open xhr 49 this._xmlHttp.open(this._method,url,true); 50 } 51 catch(e) { 52 console.warn('Not able to open XHR connection to: ' + url); 53 return; 54 } 55 56 // if post method, set different header 57 if (this._method === 'POST') { 58 this._xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 59 } 60 61 // we are now loading 62 this._loading = true; 63 Observer.publish(this,'load'); 64 65 // send data 66 this._xmlHttp.send(post); 67 }; 68 69 70 p.isLoading = function() { 71 return this._loading; 72 }; 73 74 75 p.abort = function() { 76 if (this._loading) { 77 this._xmlHttp.abort(); 78 this._aborted(); 79 } 80 }; 81 82 83 p.onReadyStateChange = function(e) { 84 85 switch(this._xmlHttp.readyState) { 86 case 1: 87 case 2: 88 case 3: { 89 // do nothing for now 90 return; 91 } 92 break; 93 case 4: { 94 95 // let's check http status 96 if (this._xmlHttp.status === 200 || 97 this._xmlHttp.status == 304 && 98 this._xmlHttp.responseText) { 99 this._success(this._xmlHttp.responseText); 100 } 101 // something went wrong 102 else { 103 this._failure(); 104 } 105 } 106 break; 107 } 108 109 }; 110 111 p.handleEvent = function(e) { 112 113 switch(this._xmlHttp.readyState) { 114 case 1: 115 case 2: 116 case 3: { 117 // do nothing for now 118 return; 119 } 120 break; 121 case 4: { 122 123 // let's check http status 124 if (this._xmlHttp.status === 200 || this._xmlHttp.status == 304 && this._xmlHttp.responseText) { 125 this._success(this._xmlHttp.responseText); 126 } 127 // something went wrong 128 else { 129 this._failure(); 130 } 131 } 132 break; 133 } 134 135 }; 136 137 p._aborted = function() { 138 139 Observer.publish(this, 'aborted'); 140 141 }; 142 143 p._failure = function() { 144 145 this._loading = false; 146 147 Observer.publish(this, 'failure'); 148 149 }; 150 151 p._success = function(response) { 152 153 this._loading = false; 154 155 // check if json response 156 try { 157 response = JSON.parse(response); 158 } 159 catch(e) { 160 // no json, maybe do something in the future 161 } 162 163 // successful request 164 Observer.publish(this, 'success', response); 165 166 }; 167 168 p.unload = function() { 169 170 this._loading = false; 171 172 this._xmlHttp.removeEventListener('readystatechange',this); 173 174 }; 175 176 177 // Register class 178 Namespace.register(ns).Request = Request; 179 180 181}('mira.data')); 182
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.