forked from requirejs/requirejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.html
More file actions
137 lines (104 loc) · 6.94 KB
/
jquery.html
File metadata and controls
137 lines (104 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<div id="directory" class="section">
<h1>How to use RequireJS with jQuery</h1>
<ul class="index mono">
<li class="hbox">
<a href="#1">Get RequireJS</a><span class="spacer boxFlex"></span><span class="sect">§ 1</span>
</li>
<li class="hbox">
<a href="#2">Set up your HTML page</a><span class="spacer boxFlex"></span><span class="sect">§ 2</span>
</li>
<li class="hbox">
<a href="#3">Feel the need for speed</a><span class="spacer boxFlex"></span><span class="sect">§ 3</span>
</li>
</ul>
<p>When a project reaches a certain size, managing the script modules for a project starts to get tricky. You need to be sure to sequence the scripts in the right order, and you need to start seriously thinking about combining scripts together into a bundle for deployment, so that only one or a very small number of requests are made to load the scripts.</p>
<p>You may also want to load code on the fly, after page load.</p>
<p>RequireJS can help you manage the script modules, load them in the right order, and make it easy to combine the scripts later via the RequireJS <a href="optimization.md">optimizer</a> without needing to change your markup. It also gives you an easy way to load scripts after the page has loaded, allowing you to spread out the download size over time.</p>
<p>RequireJS has a module system that lets you define well-scoped modules, but you do not have to follow that system to get the benefits of dependency management and build-time optimizations. Over time, if you start to create more modular code that needs to be reused in a few places, the module format for RequireJS makes it easy to write encapsulated code that can be loaded on the fly. It can grow with you, particularly if you want to incorporate internationalization (i18n) string bundles, to localize your project for different languages, or load some HTML strings and make sure those strings are available before executing code, or even use JSONP services as dependencies.</p>
</div>
<div class="section">
<h2>
<a name="1">Get RequireJS</a>
<span class="sectionMark">§ 1</span>
</h2>
<p>First step is to get RequireJS:</p>
<ul>
<li><a href="download.html#samplejquery">download the sample project</a>.</li>
</ul>
<p>This sample project uses a combined jQuery+RequireJS file. If you prefer to not bundle jQuery with RequireJS, the <a href="https://github.com/jrburke/require-jquery">sample project's README</a> has an alternate way to use jQuery with RequireJS, but it requires consideration of more edge cases.</p>
</div>
<div class="section">
<h2>
<a name="2">Set up your HTML page</a>
<span class="sectionMark">§ 2</span>
</h2>
<p>A sample HTML page would look like this (assuming you put all your .js files in a "scripts" subdirectory):</p>
<pre><code><!DOCTYPE html>
<html>
<head>
<title>jQuery+RequireJS Sample Page</title>
<script data-main="scripts/main" src="scripts/require-jquery.js"></script>
</head>
<body>
<h1>jQuery+RequireJS Sample Page</h1>
</body>
</html>
</code></pre>
<span class="note">The path rules used for data-main changed in RequireJS 0.23. Before that version, data-main="main" for the above example.</span>
<p>The data-main attribute on the script tag for require.js tells RequireJS to load the scripts/main.js file. RequireJS will load any dependency that is passed to require() without a ".js" file from the same directory as the one used for data-main. If you feel more comfortable specifying the whole path, you can also do the following:</p>
<pre><code><script data-main="scripts/main.js" src="scripts/require-jquery.js"></script>
</code></pre>
<p>What is in main.js? A call to require() to load all the scripts you need and any init work you want to do for the page. This example main.js script loads two plugins, jquery.alpha.js and jquery.beta.js (not the names of real plugins, just an example). The plugins should be in the same directory as require-jquery.js:</p>
<p>main.js:</p>
<pre><code>require(["jquery", "jquery.alpha", "jquery.beta"], function($) {
//the jquery.alpha.js and jquery.beta.js plugins have been loaded.
$(function() {
$('body').alpha().beta();
});
});
</code></pre>
<p>In the require() call, "jquery" was referenced as a dependency. The require-jquery.js file has special code in it to register jQuery as a RequireJS module, and since it is registered as a module, you can get a handle on that module as the first argument to the function callback passed to require. In the above example, $ is used to reference the jQuery module.</p>
<p>Since jquery.alpha and jquery.beta are just plugins that augment jQuery, they do not return modules that you can reference as function arguments.</p>
</div>
<div class="section">
<h2>
<a name="3">Feel the need for speed</a>
<span class="sectionMark">§ 3</span>
</h2>
<p>Now your page is set up to be optimized very easily via the <a href="optimization.html">optimizer</a>.</p>
<p>If you downloaded the jQuery sample project, then the optimizer that is part of the RequireJS source is already included.</p>
<p>If you are not using the jQuery sample project, get the optimizer by downloading the <a href="download.html#rjs">r.js file</a> and place it anywhere you like outside your web development area.</p>
<p>For the purposes of this example, the RequireJS source is placed as a sibling to the <strong>webapp</strong> directory, which contains the HTML page and the scripts directory with all the scripts. Complete directory structure:</p>
<ul>
<li>r.js (the file that includes the optimizer)</li>
<li>webapp/app.html</li>
<li>webapp/scripts/main.js</li>
<li>webapp/scripts/require-jquery.js</li>
<li>webapp/scripts/jquery.alpha.js</li>
<li>webapp/scripts/jquery.beta.js</li>
</ul>
<p>Then, in the scripts directory that has require-jquery.js and main.js, create a file called app.build.js with the following contents:</p>
<pre><code>({
appDir: "../",
baseUrl: "scripts",
dir: "../../webapp-build",
//Comment out the optimize line if you want
//the code minified by UglifyJS.
optimize: "none",
paths: {
"jquery": "require-jquery"
},
modules: [
{
name: "main",
exclude: ["jquery"]
}
]
})
</code></pre>
<p>To use the optimizer, you need <a href="http://nodejs.org">Node</a> or Java 6 installed. These instructions assume Node is being used. See the <a href="optimization.html">Optimization page</a> for more information.</p>
<p>To start the build, go to the <strong>webapp/scripts</strong> directory, execute the following command:</p>
<pre><code>node ../../r.js -o app.build.js
</code></pre>
<p>Now, in the webapp-build directory, main.js will have the main.js contents, jquery.alpha.js and jquery.beta.js inlined. If you then load the app.html file in the webapp-build directory, you should not see any network requests for jquery.alpha.js and jquery.beta.js.</p>
</div>