5
<title>YUI Loader</title>
6
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Maven+Pro:400,700">
7
<link rel="stylesheet" href="../../build/cssgrids/grids-min.css">
8
<link rel="stylesheet" href="../assets/css/main.css">
9
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
10
<script src="../../build/yui/yui-min.js"></script>
18
<a href="#toc" class="jump">Jump to Table of Contents</a>
22
<div class="yui3-u-3-4">
24
<div class="content"><div class="intro">
26
Loader dynamically loads script and css files for YUI modules as well as external modules.
27
It includes the dependency
28
infomation for the version of the library in use, and will automatically pull in
29
dependencies for the modules requested. It can load the
30
files from the Yahoo! CDN as well as local combo servers.
34
<h2 id="configuration-options">Configuration Options</h2>
36
<p>The valid configuration options for Loader are as follows:<p>
38
<li><code>lang</code>: The list of preferred languages, as BCP 47 language tags, in order of preference.
39
The loader uses this list to determine the best language to use for modules with
40
language sensitive behavior and to load the necessary resource bundles.
41
See the <a href="../intl/">Internationalization</a> module for more information.</li>
42
<li><code>base</code>: The base dir</li>
43
<li><code>comboBase</code>: The YUI combo service base dir. Ex: http://yui.yahooapis.com/combo?</li>
44
<li><code>maxURLLength</code>: The maximum URL length a combo url should be before it's chopped into a separate request.</li>
45
<li><code>comboSep</code>: The separator to use to build the combo request (defaults to &)</li>
46
<li><code>root</code>: The root path to prepend to module names for the combo service. Ex: 2.5.2/build/</li>
47
<li><code>filter</code>: A filter to apply to result urls. This filter will modify the default
48
path for all modules. The default path for the YUI library is the
49
minified version of the files (e.g., event-min.js). The filter property
50
can be a predefined filter or a custom filter. The valid predefined
53
<li><code>DEBUG</code>: Selects the debug versions of the library (e.g., event-debug.js). </li>
54
<li><code>RAW</code>: Selects the non-minified version of the library (e.g., event.js).</li>
56
You can also define a custom filter, which must be an object literal
57
containing a search expression and a replace string:
60
'searchExp': "-min\\.js",
61
'replaceStr': "-debug.js"
65
<li><code>combine</code>: Use the YUI combo service to reduce the number of http connections required to load your dependencies</li>
66
<li><code>ignore</code>: A list of modules that should never be dynamically loaded</li>
67
<li><code>force</code>: A list of modules that should always be loaded when required, even if already present on the page</li>
68
<li><code>insertBefore</code>: Node or id for a node that should be used as the insertion point for new nodes</li>
69
<li><code>charset</code>: charset for dynamic nodes</li>
70
<li><code>jsAttributes</code>: attributes to apply to dynamic script nodes</li>
71
<li><code>cssAttributes</code>: attributes to apply to dynamic link nodes</li>
72
<li><code>timeout</code>: number of milliseconds before a timeout occurs when dynamically loading nodes. in not set, there is no timeout</li>
73
<li><code>context</code>: execution context for all callbacks</li>
74
<li><code>modules</code>: A list of module definitions. The valid module configuration data is as follows:
76
<li><code>name</code>: required, the component name</li>
77
<li><code>type</code>: required, the component type (js or css)</li>
78
<li><code>path</code>: required if fullpath is not specified, the path to the script from "base"</li>
79
<li><code>fullpath</code>: required if path isn't specified, the full path to the script. "base" will not be used to build the url</li>
80
<li><code>requires</code>: array of modules required by this component</li>
81
<li><code>optional</code>: array of optional modules for this component</li>
82
<li><code>supersedes</code>: array of the modules this component replaces</li>
83
<li><code>after</code>: array of modules the components which, if present, should be sorted above this one</li>
84
<li><code>rollup</code>: the number of superseded modules required for automatic rollup</li>
85
<li><code>lang</code>: array of BCP 47 language tags of languages for which this module has localized resource bundles</li>
86
<li><code>condition</code>: Specifies that the module should be loaded automatically if
87
a condition is met. This is an object with up to three fields:
89
<li><code>trigger</code> - the name of a module that can trigger the auto-load</li>
90
<li><code>test</code> - a function that returns true when the module is to be loaded.</li>
91
<li><code>when</code> - specifies the load order of the conditional module with regard to the position of the trigger module.
92
This should be one of three values: <code>before</code>, <code>after</code>, or <code>instead</code>. The default is <code>after</code>.</li>
97
<li><code>groups</code>: in 3.1.0, the groups config was added as an enhancement over the 'modules' config. Each group can override
98
the <code>base</code>, <code>comboBase</code>, <code>root</code>, <code>combine</code>, <code>maxURLLength</code>, <code>comboSep</code> and <code>modules</code>
99
configs listed above. These values are used for all of the modules listed in the group</li>
102
<h2 id="using-loader-on-nodejs">Using Loader on Node.js</h2>
105
Loader can be used on Node.js for offline dependency calculations as well as dynamic file serving.
108
<pre class="code prettyprint">var loader = new Y.Loader({
109
//Don't combine the files
111
//Ignore things that are already loaded (in this process)
112
ignoreRegistered: true,
113
//Set the base path
114
base: 'build/'
115
//And the root
117
//Require your deps
118
require: [ 'node', 'yql' ]
121
var out = loader.resolve(true);
123
//This will be an object of js and css files needed to complete this request.
124
console.log(out);</pre>
127
<h3 id="custom-modules">Custom Modules</h3>
129
You can also use custom YUI modules as well as core YUI modules:
132
<pre class="code prettyprint">var loader = new Y.Loader({
135
fullpath: path.join(__dirname, './foo.js')
138
require: ['foo']
142
<h3 id="external-modules">External Modules</h3>
144
And you can even use external modules:
147
<pre class="code prettyprint">#!/usr/bin/env node
149
var Y = require('./yui-min').YUI();
151
var loader = new Y.Loader({
155
path: '/vendor/jquery.js'
158
path: '/vendor/backbone.js',
159
requires: [ 'jquery' ]
164
loader.require('backbone');
166
var out = loader.resolve(true);
173
[ '/Users/davglass/src/loader/vendor/jquery.js',
174
'/Users/davglass/src/loader/vendor/backbone.js' ],
181
Using this approach, you can build CLI tools to concat files for deployment or even use query string parameters
182
passed to a web app to return the content.
185
<h2 id="example-config">Example config</h2>
187
<pre class="code prettyprint">YUI({
188
lang: 'ko-KR,en-GB,zh-Hant-TW', // languages in order of preference
189
base: '../../build/', // the base path to the YUI install. Usually not needed because the default is the same base path as the yui.js include file
190
charset: 'utf-8', // specify a charset for inserted nodes, default is utf-8
191
loadOptional: true, // automatically load optional dependencies, default false
192
combine: true, // use the Yahoo! CDN combo service for YUI resources, default is true unless 'base' has been changed
193
filter: 'raw', // apply a filter to load the raw or debug version of YUI files
194
timeout: 10000, // specify the amount of time to wait for a node to finish loading before aborting
195
insertBefore: 'customstyles', // The insertion point for new nodes
196
// one or more external modules that can be loaded along side of YUI. This is the only pattern
197
// that was supported in 3.0.0 for declaring external modules. 3.1.0 adds 'groups' support,
198
// which is an easier way to define a group of modules. See below.
200
yui2_yde_datasource: {
201
fullpath: 'http://yui.yahooapis.com/combo?2.7.0/build/yahoo-dom-event/yahoo-dom-event.js&2.7.0/build/datasource/datasource-min.js'
204
fullpath: 'http://bluesmoon.github.com/yui-flot/yui.flot.js',
205
requires: ['yui2_yde_datasource']
209
// one or more groups of modules which share the same base path and
210
// combo service specification.
212
// Note, while this is a valid way to load YUI2, 3.1.0 has intrinsic
213
// YUI 2 loading built in. See the examples to learn how to use
214
// this feature.
217
base: 'http://yui.yahooapis.com/2.8.0r4/build/',
218
comboBase: 'http://yui.yahooapis.com/combo?',
219
root: '2.8.0r4/build/',
220
modules: { // one or more external modules that can be loaded along side of YUI
222
path: "yahoo-dom-event/yahoo-dom-event.js"
225
path: "animation/animation.js",
226
requires: ['yui2_yde']
231
}).use('dd', 'yui_flot', function(Y) {
232
// All YUI modules required to get drag and drop to work are now loaded.
241
<div class="yui3-u-1-4">
242
<div class="sidebar">
244
<div id="toc" class="sidebox">
246
<h2 class="no-toc">Table of Contents</h2>
252
<a href="#configuration-options">Configuration Options</a>
255
<a href="#using-loader-on-nodejs">Using Loader on Node.js</a>
258
<a href="#custom-modules">Custom Modules</a>
261
<a href="#external-modules">External Modules</a>
266
<a href="#example-config">Example config</a>
274
<div class="sidebox">
276
<h2 class="no-toc">Examples</h2>
280
<ul class="examples">
283
<li data-description="Setting up a YUI Instance">
284
<a href="yui-core.html">YUI Core</a>
289
<li data-description="Working with multiple YUI instances.">
290
<a href="yui-multi.html">Multiple Instances</a>
295
<li data-description="On-demand loading of YUI and non-YUI assets">
296
<a href="yui-loader-ext.html">YUI Loader - Dynamically Adding YUI and External Modules</a>
301
<li data-description="Create Class Hierarchies with `extend`">
302
<a href="yui-extend.html">Create Class Hierarchies with `extend`</a>
307
<li data-description="Creating a composition-based class structure using `augment`">
308
<a href="yui-augment.html">Compose Classes of Objects with `augment`</a>
313
<li data-description="Add behaviors to objects or static classes with `mix`">
314
<a href="yui-mix.html">Add Behaviors to Objects with `mix`</a>
319
<li data-description="Combine data sets and create shallow copies of objects with `merge`">
320
<a href="yui-merge.html">Combine Data Sets with `merge`</a>
325
<li data-description="Check data types with the `Lang Utilities`">
326
<a href="yui-isa.html">Check Data Types with `Lang`</a>
331
<li data-description="Get information about the current user agent with `UA`">
332
<a href="yui-ua.html">Browser Detection with `UA`</a>
337
<li data-description="Working with YUI 2 in 3">
338
<a href="yui-yui2.html">Working with YUI 2 in 3</a>
343
<li data-description="Natively use YUI Gallery Modules">
344
<a href="yui-gallery.html">Natively use YUI Gallery Modules</a>
349
<li data-description="Programatically use Loader">
350
<a href="loader-resolve.html">Programatically use Loader</a>
355
<li data-description="Executing functions in parallel">
356
<a href="parallel.html">Using Y.Parallel</a>
371
<script src="../assets/vendor/prettify/prettify-min.js"></script>
372
<script>prettyPrint();</script>