index.mustache revision 54d9f2784fc7e4f704c3efbfdf67cf74f41c0ca3
03831d35f7499c87d51205817c93e9a8d42c4baestevel<div class="intro">
03831d35f7499c87d51205817c93e9a8d42c4baestevel The `YUI` module is the core of YUI 3. It must be included on all pages that use YUI, and it's the only dependency required to start writing YUI code. The YUI module contains loader functionality and a dependency calculator, allowing it to serve as a "seed" that can load other dependencies as needed.
03831d35f7499c87d51205817c93e9a8d42c4baestevel<h2>Getting Started</h2>
03831d35f7499c87d51205817c93e9a8d42c4baestevelThe first step in using YUI is to load the YUI "seed". The seed is the bare minimum core code that's needed to allow YUI to dynamically load additional dependencies on demand. YUI is extremely modular, and the small seed file makes it easy to load only the modules you want to use on a given page.
03831d35f7499c87d51205817c93e9a8d42c4baestevelInclude the YUI seed file by adding this script tag to your HTML:
03831d35f7499c87d51205817c93e9a8d42c4baestevel<script src="http://yui.yahooapis.com/{{{yuiVersion}}}/build/yui/yui-min.js"></script>
03831d35f7499c87d51205817c93e9a8d42c4baestevelThe seed file adds a single global variable to the page: the `YUI` object. To begin using YUI, you'll first create a new YUI instance and then tell that instance which YUI modules you want to use.
03831d35f7499c87d51205817c93e9a8d42c4baestevel<div id="demo">Click me</div>
03831d35f7499c87d51205817c93e9a8d42c4baestevel// Create a new YUI sandbox and load the "node" module.
03831d35f7499c87d51205817c93e9a8d42c4baestevelYUI().use('node', function (Y) {
03831d35f7499c87d51205817c93e9a8d42c4baestevel // YUI will call this function and pass in the YUI instance (Y) once all
03831d35f7499c87d51205817c93e9a8d42c4baestevel // modules have finished loading and are ready to use.
03831d35f7499c87d51205817c93e9a8d42c4baestevel // We can now use Y.Node to get references to DOM elements using CSS
03831d35f7499c87d51205817c93e9a8d42c4baestevel // selectors.
03831d35f7499c87d51205817c93e9a8d42c4baestevel var demo = Y.one('#demo');
03831d35f7499c87d51205817c93e9a8d42c4baestevel // And we can listen for DOM events.
03831d35f7499c87d51205817c93e9a8d42c4baestevel demo.on('click', function (e) {
03831d35f7499c87d51205817c93e9a8d42c4baestevel demo.set('text', 'You clicked me!');
03831d35f7499c87d51205817c93e9a8d42c4baestevelCalling `YUI()` creates a brand new YUI instance without any active modules. We then call `.use()` on that new instance and pass in a list of modules we want to use, in the form of string parameters. You can name as many modules as you like here. Finally, we pass a callback function that will be executed once all those modules have finished loading. The callback function receives the YUI instance as an argument, which we've named `Y`.
03831d35f7499c87d51205817c93e9a8d42c4baestevelThis pattern is called a "sandbox", and it's the most important concept to understand about YUI. It not only makes it easy to load dependencies on demand, it also ensures that your code (and YUI's code!) doesn't pollute the global scope of the page or interfere with other global JavaScript you may be using.
03831d35f7499c87d51205817c93e9a8d42c4baestevelThis also means that you can have multiple YUI sandboxes on the same page, and they won't interfere with each other (but they <em>will</em> avoid reloading module code that has already been loaded).
03831d35f7499c87d51205817c93e9a8d42c4baestevel<h2>Alternative Seed Files</h2>
03831d35f7499c87d51205817c93e9a8d42c4baestevelIn [[#Getting Started]], we described the most common way to load YUI using what we call the "Loader Seed". This is a seed file that contains both the core of YUI and the code for the YUI Loader and all the metadata that's necessary to dynamically load additional YUI modules. Depending on your needs, you may want to use a different seed file to further optimize how you load YUI.
03831d35f7499c87d51205817c93e9a8d42c4baestevel<h3 id="base-seed">The Base Seed</h3>
03831d35f7499c87d51205817c93e9a8d42c4baestevel<script src="http://yui.yahooapis.com/{{{yuiVersion}}}/build/yui-base/yui-base-min.js"></script>
03831d35f7499c87d51205817c93e9a8d42c4baestevelThe base seed contains the YUI core and the <a href="../get/index.html">Get Utility</a>, but doesn't include Loader or any module metadata. The first time you call `YUI().use()`, it will automatically fetch Loader and the module metadata, and then will make a second request to fetch any additional modules you've asked for.
03831d35f7499c87d51205817c93e9a8d42c4baestevelThis results in a smaller initial seed file that can speed up the initial page load, but requires more requests overall to get an actual YUI instance up and running. Prior to version 3.4.0, this was the default YUI seed file.
7014882c6a3672fd0e5d60200af8643ae53c5928Richard Lowe<h3 id="core-seed">The Core Seed</h3>
03831d35f7499c87d51205817c93e9a8d42c4baestevel<script src="http://yui.yahooapis.com/{{{yuiVersion}}}/build/yui-core/yui-core-min.js"></script>
03831d35f7499c87d51205817c93e9a8d42c4baestevelThe core seed contains only the YUI core, and isn't capable of dynamically loading other modules. This is the smallest of all the seed files, but requires you to manually load any dependencies you need before using them.
03831d35f7499c87d51205817c93e9a8d42c4baestevel<h2>Loading Modules</h2>
03831d35f7499c87d51205817c93e9a8d42c4baestevel<h3>Dynamic Loading with `use()`</h3>
03831d35f7499c87d51205817c93e9a8d42c4baestevelThe `use()` method allows you to specify the modules that you want to load into your YUI instance.
03831d35f7499c87d51205817c93e9a8d42c4baestevelYUI().use('node', 'event', function (Y) {
03831d35f7499c87d51205817c93e9a8d42c4baestevel // The node and event modules are available on this YUI instance.
03831d35f7499c87d51205817c93e9a8d42c4baestevelYUI modules aren't actually executed until they're used and attached to a YUI instance, and two different YUI instances can have two different sets of modules attached to them. Even if both instances use the same module, each instance gets its own "copy" of that module and isn't affected by changes made in another instance.
03831d35f7499c87d51205817c93e9a8d42c4baestevelYUI().use('node', function (Y) {
03831d35f7499c87d51205817c93e9a8d42c4baestevel // We can blow away the Y.Node module in this outer YUI instance...
03831d35f7499c87d51205817c93e9a8d42c4baestevel YUI().use('node', function (Y2) {
03831d35f7499c87d51205817c93e9a8d42c4baestevel // ...without affecting it inside another YUI instance...
You can also call `use()` on an existing YUI instance to attach more modules to that instance without needing to create a completely new YUI instance. This is useful for lazy-loading modules that aren't needed up front.
Y.use('autocomplete', function () {
The `YUI().use()` call might seem like magic, but it's actually doing something very simple. It's easier to understand what's going on if we break it into multiple steps.
First, calling `YUI()` creates a brand new YUI instance. This instance will later be passed on to our callback function as the `Y` argument, but if we wanted to, we could just stop here and start using it immediately without even calling `use()`.
Next, we call `use()` on the new YUI instance that was just created. We pass in a list of the modules we want to use, followed by a function that we want YUI to call once all those modules are available.
Finally, YUI loads any necessary modules, attaches them to the YUI instance (this is when the modules are actually executed), and then calls our callback function. The YUI instance passes itself to the callback function as an argument for convenience, so that we don't have to store the instance in a global variable.
The callback function passed to `use()` is executed asynchronously, which means that it doesn't block subsequent code while modules are being loaded.
// call Y.use() here and not YUI().use().
Y.use('node', 'event', function (Y) {
<li><p><strong>Not loaded</strong>: The module code has not been downloaded yet and is not available to any YUI instance.</p></li>
<li><p><strong>Loaded</strong>: The module code has been downloaded, but has not been attached to this specific YUI instance. Other instances may be using it, but this instance isn't using it yet.</p></li>
<li><p><strong>Attached</strong>: The module code has been downloaded and is attached to this YUI instance. The module is ready to use.</p></li>
To reach the "loaded" state, a module's JavaScript just needs to be included on the page after the YUI seed file. The `use()` method will do this for you automatically if necessary, but you could also load a module manually if you wanted to. We call this static loading (since it's the opposite of dynamic loading).
If you want to take full manual control of your dependencies, you can statically load any modules you want to use and then pass `'*'` to `use()` instead of specifying a list of module names. This tells YUI to attach all loaded modules to your YUI instance without requiring you to name each module you want to attach.
There are four primary ways to configure YUI and each has its own unique benefits. The YUI object is configured via properties on a simple JavaScript object.
comboBase: 'http://mydomain.com/combo?',
<a href="{{apiDocs}}/classes/config.html">available in the API Docs</a>.
The most common way to specify config options for YUI is to pass them into the `YUI()` constructor when creating a new instance:
comboBase: 'http://mydomain.com/combo?',
comboBase: 'http://mydomain.com/combo?',
<h3>YUI.GlobalConfig</h3>
By setting options on the `YUI.GlobalConfig` object, you can configure every YUI
YUI.GlobalConfig = {
comboBase: 'http://mydomain.com/combo?',
<h3>YUI.applyConfig</h3>
The global `YUI.applyConfig()` method allows you to configure every YUI instance on the page, but it <em>merges</em> configs passed to it into each instance's existing config. This can be useful if your module is loaded onto the page in a <em>mashup</em>. The other configuration options do not merge, they are simply an object.
comboBase: 'http://mydomain.com/combo?',
<h2 id="yuiadd">Creating Custom Modules with `YUI.add()`</h2>
<p>For more information on creating your custom modules, see our <a href="create.html">Creating YUI Modules</a> example.</p>
<h2 id="nodejs">Using YUI on Node.js</h2>
<p>As of version 3.5.0, YUI runs natively on <a href="http://nodejs.org/">Node.js</a> and comes with an official <a href="http://search.npmjs.org/#/yui">npm package</a> for easy installation. More information on using YUI on Node.js can be found in the <a href="nodejs.html">YUI on Node.js guide</a>.</p>
<p><a href="loader.html">Loader</a>'s functionality is now built into the YUI Global Object
<p>You can find <a href="loader.html">more information about Loader here</a>.</p>
<h2 id="Lang">Y.Lang</h2>
<p>`Y.Lang` contains JavaScript language utilities and extensions that are used in the YUI library.
YUI provides more than 250 unique modules to use in your applications. <a href="modules.html">You can view a full list of modules here.</a>