Permalink
Browse files

Relates to #13, roll back the change to #11, but introduce a config.e…

…nv override to help address #11.
  • Loading branch information...
1 parent e705b16 commit 6485a2c8aa3a67ca9098df3e2826c6b76e843925 @jrburke jrburke committed Aug 12, 2012
Showing with 56 additions and 34 deletions.
  1. +19 −0 README.md
  2. +37 −34 text.js
View
@@ -160,6 +160,25 @@ requirejs.config({
});
```
+### Forcing the environment implemention
+
+The text plugin tries to detect what environment it is available for loading
+text resources, Node, XMLHttpRequest (XHR) or Rhino, but sometimes the
+Node or Rhino environment may have loaded a library that introduces an XHR
+implementation. You can foce the environment implementation to use by passing
+an "env" module config to the plugin:
+
+```javascript
+requirejs.config({
+ config: {
+ text: {
+ //Valid values are 'node', 'xhr', or 'rhino'
+ env: 'rhino'
+ }
+ }
+});
+```
+
## License
Dual-licensed -- new BSD or MIT.
View
71 text.js
@@ -1,5 +1,5 @@
/**
- * @license RequireJS text 2.0.2 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
+ * @license RequireJS text 2.0.2+ Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/requirejs/text for details
*/
@@ -23,7 +23,7 @@ define(['module'], function (module) {
masterConfig = (module.config && module.config()) || {};
text = {
- version: '2.0.2',
+ version: '2.0.2+',
strip: function (content) {
//Strips <?xml ...?> declarations so that external SVG and XML
@@ -219,9 +219,10 @@ define(['module'], function (module) {
}
};
- if (typeof process !== "undefined" &&
- process.versions &&
- !!process.versions.node) {
+ if (masterConfig.env === 'node' || (!masterConfig.env &&
+ typeof process !== "undefined" &&
+ process.versions &&
+ !!process.versions.node)) {
//Using special require.nodeRequire, something added by r.js.
fs = require.nodeRequire('fs');
@@ -233,7 +234,37 @@ define(['module'], function (module) {
}
callback(file);
};
- } else if (typeof Packages !== 'undefined' && typeof java !== 'undefined') {
+ } else if (masterConfig.env === 'xhr' || (!masterConfig.env &&
+ text.createXhr())) {
+ text.get = function (url, callback, errback) {
+ var xhr = text.createXhr();
+ xhr.open('GET', url, true);
+
+ //Allow overrides specified in config
+ if (masterConfig.onXhr) {
+ masterConfig.onXhr(xhr, url);
+ }
+
+ xhr.onreadystatechange = function (evt) {
+ var status, err;
+ //Do not explicitly handle errors, those should be
+ //visible via console output in the browser.
+ if (xhr.readyState === 4) {
+ status = xhr.status;
+ if (status > 399 && status < 600) {
+ //An http 4xx or 5xx error. Signal an error.
+ err = new Error(url + ' HTTP status: ' + status);
+ err.xhr = xhr;
+ errback(err);
+ } else {
+ callback(xhr.responseText);
+ }
+ }
+ };
+ xhr.send(null);
+ };
+ } else if (masterConfig.env === 'rhino' || (!masterConfig.env &&
+ typeof Packages !== 'undefined' && typeof java !== 'undefined')) {
//Why Java, why is this so awkward?
text.get = function (url, callback) {
var stringBuffer, line,
@@ -271,34 +302,6 @@ define(['module'], function (module) {
}
callback(content);
};
- } else if (text.createXhr()) {
- text.get = function (url, callback, errback) {
- var xhr = text.createXhr();
- xhr.open('GET', url, true);
-
- //Allow overrides specified in config
- if (masterConfig.onXhr) {
- masterConfig.onXhr(xhr, url);
- }
-
- xhr.onreadystatechange = function (evt) {
- var status, err;
- //Do not explicitly handle errors, those should be
- //visible via console output in the browser.
- if (xhr.readyState === 4) {
- status = xhr.status;
- if (status > 399 && status < 600) {
- //An http 4xx or 5xx error. Signal an error.
- err = new Error(url + ' HTTP status: ' + status);
- err.xhr = xhr;
- errback(err);
- } else {
- callback(xhr.responseText);
- }
- }
- };
- xhr.send(null);
- };
}
return text;

0 comments on commit 6485a2c

Please sign in to comment.