From a3edb5156a2554a663dba72ff504cdc3f9208ffa Mon Sep 17 00:00:00 2001 From: Lee Goddard Date: Mon, 8 Oct 2012 10:09:20 +0200 Subject: [PATCH 1/3] Added to Request.js support for XHR Level 2 resposneType of arrayBuffer or blob. --- Docs/Request/Request.md | 14 ++++++++++++++ Source/Request/Request.js | 25 +++++++++++++++++++------ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/Docs/Request/Request.md b/Docs/Request/Request.md index bf05d8c4c..39e7aeb62 100644 --- a/Docs/Request/Request.md +++ b/Docs/Request/Request.md @@ -30,6 +30,7 @@ An XMLHttpRequest Wrapper. * timeout - (*integer*: defaults to 0) In conjunction with `onTimeout` event, it determines the amount of milliseconds before considering a connection timed out. (It's suggested to not use timeout with big files and only when knowing what's expected.) * headers - (*object*) An object to use in order to set the request headers. * urlEncoded - (*boolean*: defaults to *true*) If set to true, the content-type header is set to www-form-urlencoded + encoding +* responseType - (*string*: defaults to *null*, may be *arrayBuffer* or *blob*) Provides access to the XHR Level 2 *responseType*, allowing requests of binary data. * encoding - (*string*: defaults to 'utf-8') The encoding to be set in the request header. * noCache - (*boolean*; defaults to *false*) If *true*, appends a unique *noCache* value to the request to prevent caching. (IE has a bad habit of caching ajax request values. Including this script and setting the *noCache* value to true will prevent it from caching. The server should ignore the *noCache* value.) * isSuccess - (*function*) Overrides the built-in isSuccess function. @@ -87,10 +88,23 @@ Fired when the Request is making progresses in the download or upload. (This is myRequest.send(); + var mySound = new Request({ + method: 'get', + url: 'squarepusher.wav', + responseType: 'arraybuffer', + onSuccess: function(res){ + myAudioContext.decodeAudioData(res, function(buffer){ + if (buffer) myAudioBuffer = buffer; + }); + } + }).send(); + ### See Also: - [MDC: nsIDOMProgressEvent](https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIDOMProgressEvent) + - [W3C: XHR Level 2](http://www.w3.org/TR/XMLHttpRequest/) + #### complete Fired when the Request is completed. diff --git a/Source/Request/Request.js b/Source/Request/Request.js index b18ba4435..516440692 100644 --- a/Source/Request/Request.js +++ b/Source/Request/Request.js @@ -74,11 +74,23 @@ var Request = this.Request = new Class({ if (progressSupport) xhr.onprogress = xhr.onloadstart = empty; clearTimeout(this.timer); - this.response = {text: this.xhr.responseText || '', xml: this.xhr.responseXML}; - if (this.options.isSuccess.call(this, this.status)) - this.success(this.response.text, this.response.xml); - else - this.failure(); + if (this.options.responseType + && (this.options.responseType == 'arraybuffer' || this.options.responseType == 'blob') + ){ + this.response = {}; + this.response[this.options.responseType] = this.xhr.response || ''; + if (this.options.isSuccess.call(this, this.status)) + this.success(this.response[this.options.responseType]); + else + this.failure(); + } + else { + this.response = {text: this.xhr.responseText || '', xml: this.xhr.responseXML}; + if (this.options.isSuccess.call(this, this.status)) + this.success(this.response.text, this.response.xml); + else + this.failure(); + } }, isSuccess: function(){ @@ -91,6 +103,7 @@ var Request = this.Request = new Class({ }, processScripts: function(text){ + if (typeof text != 'string') return text; if (this.options.evalResponse || (/(ecma|java)script/).test(this.getHeader('Content-type'))) return Browser.exec(text); return text.stripScripts(this.options.evalScripts); }, @@ -198,7 +211,7 @@ var Request = this.Request = new Class({ xhr.open(method.toUpperCase(), url, this.options.async, this.options.user, this.options.password); if (this.options.user && 'withCredentials' in xhr) xhr.withCredentials = true; - + if (this.options.responseType) xhr.responseType = this.options.responseType; xhr.onreadystatechange = this.onStateChange.bind(this); Object.each(this.headers, function(value, key){ From fbb9c954f68185dded59ff1cbc8f9fc30396851f Mon Sep 17 00:00:00 2001 From: Lee Goddard Date: Fri, 4 Jan 2013 14:21:13 +0100 Subject: [PATCH 2/3] Whitespace formatting --- Docs/Request/Request.md | 1 - Source/Request/Request.js | 6 ++---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/Docs/Request/Request.md b/Docs/Request/Request.md index 39e7aeb62..fa464933b 100644 --- a/Docs/Request/Request.md +++ b/Docs/Request/Request.md @@ -81,7 +81,6 @@ Fired when the Request is making progresses in the download or upload. (This is url: 'image.jpg', onProgress: function(event, xhr){ var loaded = event.loaded, total = event.total; - console.log(parseInt(loaded / total * 100, 10)); } }); diff --git a/Source/Request/Request.js b/Source/Request/Request.js index 516440692..9dc691bb1 100644 --- a/Source/Request/Request.js +++ b/Source/Request/Request.js @@ -75,16 +75,14 @@ var Request = this.Request = new Class({ clearTimeout(this.timer); if (this.options.responseType - && (this.options.responseType == 'arraybuffer' || this.options.responseType == 'blob') - ){ + && (this.options.responseType == 'arraybuffer' || this.options.responseType == 'blob') ){ this.response = {}; this.response[this.options.responseType] = this.xhr.response || ''; if (this.options.isSuccess.call(this, this.status)) this.success(this.response[this.options.responseType]); else this.failure(); - } - else { + } else { this.response = {text: this.xhr.responseText || '', xml: this.xhr.responseXML}; if (this.options.isSuccess.call(this, this.status)) this.success(this.response.text, this.response.xml); From cf3351806c9f042c9a61f9444a2ea713a6ca37b9 Mon Sep 17 00:00:00 2001 From: Lee Goddard Date: Fri, 18 Jan 2013 15:00:09 +0100 Subject: [PATCH 3/3] Tidied as requested by Arian Stolwijk today --- Docs/Request/Request.md | 6 +++--- Source/Request/Request.js | 13 +++++++------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Docs/Request/Request.md b/Docs/Request/Request.md index fa464933b..62bace7cf 100644 --- a/Docs/Request/Request.md +++ b/Docs/Request/Request.md @@ -88,10 +88,10 @@ Fired when the Request is making progresses in the download or upload. (This is myRequest.send(); var mySound = new Request({ - method: 'get', - url: 'squarepusher.wav', + method: 'get', + url: 'squarepusher.wav', responseType: 'arraybuffer', - onSuccess: function(res){ + onSuccess: function(res){ myAudioContext.decodeAudioData(res, function(buffer){ if (buffer) myAudioBuffer = buffer; }); diff --git a/Source/Request/Request.js b/Source/Request/Request.js index 9dc691bb1..8ce5f5a49 100644 --- a/Source/Request/Request.js +++ b/Source/Request/Request.js @@ -74,20 +74,21 @@ var Request = this.Request = new Class({ if (progressSupport) xhr.onprogress = xhr.onloadstart = empty; clearTimeout(this.timer); - if (this.options.responseType - && (this.options.responseType == 'arraybuffer' || this.options.responseType == 'blob') ){ + if (this.options.responseType && (this.options.responseType == 'arraybuffer' || this.options.responseType == 'blob')){ this.response = {}; this.response[this.options.responseType] = this.xhr.response || ''; - if (this.options.isSuccess.call(this, this.status)) + if (this.options.isSuccess.call(this, this.status)){ this.success(this.response[this.options.responseType]); - else + } else { this.failure(); + } } else { this.response = {text: this.xhr.responseText || '', xml: this.xhr.responseXML}; - if (this.options.isSuccess.call(this, this.status)) + if (this.options.isSuccess.call(this, this.status)){ this.success(this.response.text, this.response.xml); - else + } else { this.failure(); + } } },