~caneypuggies/reformedchurcheslocator/couchapp-backbone

« back to all changes in this revision

Viewing changes to _attachments/js/vendor/requirejs-text/README.md

  • Committer: Tim Black
  • Date: 2013-09-16 22:50:16 UTC
  • Revision ID: tim@alwaysreformed.com-20130916225016-zk8jiba25z33ew7h
Versioned Bower vendor directory

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# text
 
2
 
 
3
A [RequireJS](http://requirejs.org)/AMD loader plugin for loading text
 
4
resources.
 
5
 
 
6
Known to work in RequireJS, but should work in other AMD loaders that support
 
7
the same loader plugin API.
 
8
 
 
9
## Docs
 
10
 
 
11
See the [RequireJS API text section](http://requirejs.org/docs/api.html#text).
 
12
 
 
13
## Latest release
 
14
 
 
15
The latest release is always available from [the "latest" tag](https://raw.github.com/requirejs/text/latest/text.js).
 
16
 
 
17
It can also be installed using [volo](https://github.com/volojs/volo):
 
18
 
 
19
    volo add requirejs/text
 
20
 
 
21
## Usage
 
22
 
 
23
It is nice to build HTML using regular HTML tags, instead of building up DOM
 
24
structures in script. However, there is no good way to embed HTML in a
 
25
JavaScript file. The best that can be done is using a string of HTML, but that
 
26
can be hard to manage, particularly for multi-line HTML.
 
27
 
 
28
The text.js AMD loader plugin can help with this issue. It will automatically be
 
29
loaded if the text! prefix is used for a dependency. Download the plugin and put
 
30
it in the app's [baseUrl](http://requirejs.org/docs/api.html#config-baseUrl)
 
31
directory (or use the [paths config](http://requirejs.org/docs/api.html#config-paths) to place it in other areas).
 
32
 
 
33
You can specify a text file resource as a dependency like so:
 
34
 
 
35
```javascript
 
36
require(["some/module", "text!some/module.html", "text!some/module.css"],
 
37
    function(module, html, css) {
 
38
        //the html variable will be the text
 
39
        //of the some/module.html file
 
40
        //the css variable will be the text
 
41
        //of the some/module.css file.
 
42
    }
 
43
);
 
44
```
 
45
 
 
46
Notice the .html and .css suffixes to specify the extension of the file. The
 
47
"some/module" part of the path will be resolved according to normal module name
 
48
resolution: it will use the **baseUrl** and **paths** [configuration
 
49
options](http://requirejs.org/docs/api.html#config) to map that name to a path.
 
50
 
 
51
For HTML/XML/SVG files, there is another option. You can pass !strip, which
 
52
strips XML declarations so that external SVG and XML documents can be added to a
 
53
document without worry. Also, if the string is an HTML document, only the part
 
54
inside the body tag is returned. Example:
 
55
 
 
56
```javascript
 
57
require(["text!some/module.html!strip"],
 
58
    function(html) {
 
59
        //the html variable will be the text of the
 
60
        //some/module.html file, but only the part
 
61
        //inside the body tag.
 
62
    }
 
63
);
 
64
```
 
65
 
 
66
The text files are loaded via asynchronous XMLHttpRequest (XHR) calls, so you
 
67
can only fetch files from the same domain as the web page (see **XHR
 
68
restrictions** below).
 
69
 
 
70
However, [the RequireJS optimizer](http://requirejs.org/docs/optimization.html)
 
71
will inline any text! references with the actual text file contents into the
 
72
modules, so after a build, the modules that have text! dependencies can be used
 
73
from other domains.
 
74
 
 
75
## Configuration
 
76
 
 
77
### XHR restrictions
 
78
 
 
79
The text plugin works by using XMLHttpRequest (XHR) to fetch the text for the
 
80
resources it handles.
 
81
 
 
82
However, XHR calls have some restrictions, due to browser/web security policies:
 
83
 
 
84
1) Many browsers do not allow file:// access to just any file. You are better
 
85
off serving the application from a local web server than using local file://
 
86
URLs. You will likely run into trouble otherwise.
 
87
 
 
88
2) There are restrictions for using XHR to access files on another web domain.
 
89
While CORS can help enable the server for cross-domain access, doing so must
 
90
be done with care (in particular if you also host an API from that domain),
 
91
and not all browsers support CORS.
 
92
 
 
93
So if the text plugin determines that the request for the resource is on another
 
94
domain, it will try to access a ".js" version of the resource by using a
 
95
script tag. Script tag GET requests are allowed across domains. The .js version
 
96
of the resource should just be a script with a define() call in it that returns
 
97
a string for the module value.
 
98
 
 
99
Example: if the resource is 'text!example.html' and that resolves to a path
 
100
on another web domain, the text plugin will do a script tag load for
 
101
'example.html.js'.
 
102
 
 
103
The [requirejs optimizer](http://requirejs.org/docs/optimization.html) will
 
104
generate these '.js' versions of the text resources if you set this in the
 
105
build profile:
 
106
 
 
107
    optimizeAllPluginResources: true
 
108
 
 
109
In some cases, you may want the text plugin to not try the .js resource, maybe
 
110
because you have configured CORS on the other server, and you know that only
 
111
browsers that support CORS will be used. In that case you can use the
 
112
[module config](http://requirejs.org/docs/api.html#config-moduleconfig)
 
113
(requires RequireJS 2+) to override some of the basic logic the plugin uses to
 
114
determine if the .js file should be requested:
 
115
 
 
116
```javascript
 
117
requirejs.config({
 
118
    config: {
 
119
        text: {
 
120
            useXhr: function (url, protocol, hostname, port) {
 
121
                //Override function for determining if XHR should be used.
 
122
                //url: the URL being requested
 
123
                //protocol: protocol of page text.js is running on
 
124
                //hostname: hostname of page text.js is running on
 
125
                //port: port of page text.js is running on
 
126
                //Use protocol, hostname, and port to compare against the url
 
127
                //being requested.
 
128
                //Return true or false. true means "use xhr", false means
 
129
                //"fetch the .js version of this resource".
 
130
            }
 
131
        }
 
132
    }
 
133
});
 
134
```
 
135
 
 
136
### Custom XHR hooks
 
137
 
 
138
There may be cases where you might want to provide the XHR object to use
 
139
in the request, or you may just want to add some custom headers to the
 
140
XHR object used to make the request. You can use the following hooks:
 
141
 
 
142
```javascript
 
143
requirejs.config({
 
144
    config: {
 
145
        text: {
 
146
            onXhr: function (xhr, url) {
 
147
                //Called after the XHR has been created and after the
 
148
                //xhr.open() call, but before the xhr.send() call.
 
149
                //Useful time to set headers.
 
150
                //xhr: the xhr object
 
151
                //url: the url that is being used with the xhr object.
 
152
            },
 
153
            createXhr: function () {
 
154
                //Overrides the creation of the XHR object. Return an XHR
 
155
                //object from this function.
 
156
                //Available in text.js 2.0.1 or later.
 
157
            },
 
158
            onXhrComplete: function (xhr, url) {
 
159
                //Called whenever an XHR has completed its work. Useful
 
160
                //if browser-specific xhr cleanup needs to be done.
 
161
            }
 
162
        }
 
163
    }
 
164
});
 
165
```
 
166
 
 
167
### Forcing the environment implemention
 
168
 
 
169
The text plugin tries to detect what environment it is available for loading
 
170
text resources, Node, XMLHttpRequest (XHR) or Rhino, but sometimes the
 
171
Node or Rhino environment may have loaded a library that introduces an XHR
 
172
implementation. You can foce the environment implementation to use by passing
 
173
an "env" module config to the plugin:
 
174
 
 
175
```javascript
 
176
requirejs.config({
 
177
    config: {
 
178
        text: {
 
179
            //Valid values are 'node', 'xhr', or 'rhino'
 
180
            env: 'rhino'
 
181
        }
 
182
    }
 
183
});
 
184
```
 
185
 
 
186
## License
 
187
 
 
188
Dual-licensed -- new BSD or MIT.
 
189
 
 
190
## Where are the tests?
 
191
 
 
192
They are in the [requirejs](https://github.com/jrburke/requirejs) and
 
193
[r.js](https://github.com/jrburke/r.js) repos.
 
194
 
 
195
## History
 
196
 
 
197
This plugin was in the [requirejs repo](https://github.com/jrburke/requirejs)
 
198
up until the requirejs 2.0 release.