diff options
Diffstat (limited to 'lib/caldav/xhr.js')
-rw-r--r-- | lib/caldav/xhr.js | 61 |
1 files changed, 38 insertions, 23 deletions
diff --git a/lib/caldav/xhr.js b/lib/caldav/xhr.js index 77ecd5c..e88e789 100644 --- a/lib/caldav/xhr.js +++ b/lib/caldav/xhr.js @@ -43,6 +43,7 @@ Xhr.prototype = { globalXhrOptions: null, xhrClass: Native, + xhr: null, method: 'GET', async: true, waiting: false, @@ -53,7 +54,7 @@ headers: {}, data: null, - _seralize: function _seralize() { + _serialize: function _serialize() { return this.data; }, @@ -70,31 +71,45 @@ }, /** + * Aborts the request if it has already been sent. + * @param {Function=} cb An optional callback function. + */ + abort: function(cb) { + if (this.waiting) { + this.xhr.abort(); + this.waiting = false; + } + + if (cb !== undefined) { + cb(); + } + }, + + /** * Sends request to server. * * @param {Function} callback success/failure handler. */ send: function send(callback) { - var header, xhr; + var header; if (typeof(callback) === 'undefined') { callback = this.callback; } - if (this.globalXhrOptions) { - xhr = new this.xhrClass(this.globalXhrOptions); - } else { - xhr = new this.xhrClass(); - } + this.xhr = new this.xhrClass( + this.globalXhrOptions ? this.globalXhrOptions : undefined); + // This hack is in place due to some platform // bug in gecko when using mozSystem xhr // the credentials only seem to work as expected // when constructing them manually. if (!this.globalXhrOptions || !this.globalXhrOptions.mozSystem) { - xhr.open(this.method, this.url, this.async, this.user, this.password); + this.xhr.open( + this.method, this.url, this.async, this.user, this.password); } else { - xhr.open(this.method, this.url, this.async); - xhr.setRequestHeader('Authorization', this._credentials( + this.xhr.open(this.method, this.url, this.async); + this.xhr.setRequestHeader('Authorization', this._credentials( this.user, this.password )); @@ -103,12 +118,12 @@ var useMozChunkedText = false; if (this.globalXhrOptions && this.globalXhrOptions.useMozChunkedText) { useMozChunkedText = true; - xhr.responseType = 'moz-chunked-text'; + this.xhr.responseType = 'moz-chunked-text'; } for (header in this.headers) { if (Object.hasOwnProperty.call(this.headers, header)) { - xhr.setRequestHeader(header, this.headers[header]); + this.xhr.setRequestHeader(header, this.headers[header]); } } @@ -116,19 +131,19 @@ var hasProgressEvents = false; // check for progress event support. - if ('onprogress' in xhr) { + if ('onprogress' in this.xhr) { hasProgressEvents = true; var last = 0; if (useMozChunkedText) { - xhr.onprogress = (function onChunkedProgress(event) { + this.xhr.onprogress = (function onChunkedProgress(event) { if (this.ondata) { - this.ondata(xhr.responseText); + this.ondata(this.xhr.responseText); } }.bind(this)); } else { - xhr.onprogress = (function onProgress(event) { - var chunk = xhr.responseText.substr(last, event.loaded); + this.xhr.onprogress = (function onProgress(event) { + var chunk = this.xhr.responseText.substr(last, event.loaded); last = event.loaded; if (this.ondata) { this.ondata(chunk); @@ -137,10 +152,10 @@ } } - xhr.onreadystatechange = (function onReadyStateChange() { + this.xhr.onreadystatechange = (function onReadyStateChange() { var data; - if (xhr.readyState === 4) { - data = xhr.responseText; + if (this.xhr.readyState === 4) { + data = this.xhr.responseText; // emulate progress events for node... // this really lame we should probably just @@ -151,14 +166,14 @@ } this.waiting = false; - callback(null, xhr); + callback(null, this.xhr); } }.bind(this)); this.waiting = true; - xhr.send(this._seralize()); + this.xhr.send(this._serialize()); - return xhr; + return this.xhr; } }; |