YUI 3: YUI Global Object

The YUI module is the single core dependency for all YUI 3.x implementations. It must be included on all pages that use YUI — and it is 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" for your implementation. You provide the YUI module list you're using and the code that makes use of those modules; YUI will fetch all necessary components in a single, optimized HTTP request before executing your dependent code. While you may use some of the script- and CSS-loading facilities of the YUI module in your own implementation, this module's core purpose is to serve as a small seed from which complex, highly modular implementations can grow.

The YUI module creates a global YUI object. This object is instantiable, and it is used to create YUI instances to which are bound specific functional modules. A page can share a single YUI instance or can use different, insular instances for each piece of functionality on the page.

Getting Started

Include Dependencies

To use the YUI Global Object, include the YUI seed file:

Using the YUI Global Object

This section describes how to use the YUI Global Object in further detail. It contains these subsections:

Why the namespace change?

  1. Backward Compatibility: With the change from YAHOO to YUI we are guaranteed not to break backward compatibility. This allows you to use both YUI 2.x and YUI 3.x on the same page without fear of breaking existing code.
  2. Sandboxing: The new YUI Global Object is now smarter than ever, allowing you to create an instance of YUI in your own namespace and only load the modules that you need. It makes it easier for multiple developers to work on different parts of the page with different modules with less worry of them stepping on each other's toes.
  3. Versioning (future): In the future, we intend to version-stamp YUI modules; this will make it easier to support mulitple versions of YUI modules on the same page. While this will never be an optimal practice, it does make certain transitions easier to facilitate.
  4. More Dynamic: With Loader built into the core, loading files when you need them just got easier.
  5. Selector Powered: Using Selector as its base for node handling, you now get more power and ease of use for free.
  6. Event Normalization: With the new Event Facade, you get maximum event normalization for cleaner cross browser code.

YUI Core

The YUI Global Object is an instantiatable object that allows you to create as many YUI instances as you need. Each completely configurable and loaded with only the modules that you need.

All of this functionality is available in the YUI Core:

YUI 3 Component Module
Array Operationsarray
YUI Corecore
Language helper methodslang
Periodic execution methodlater
Logging supportlog
Object Operationsobject
Browser Sniffingua
YUI Objectyui
Dynamic script and css loadingget
YUI Loaderloader

Use method

The use method allows you to choose the modules that you want loaded into your YUI instance. You can pick and choose what you need, you don't have to load everything that was included on the page.

Use callback

You can pass a function in as the last argument to use. This function will execute after the YUI instance loads all the modules. This is required if you do not have all dependencies on the page.

The callback method has one argument passed, the YUI instance that we are dealing with. In this function you know that all the modules have been loaded and it's ok to use them.

Use shorthand

The use method contains a shorthand reference for all modules on the page. It uses the * as the module name.

This will load every module that is included on the page.

Note: This shorthand only loads modules that are already on the page. If you explicitly specify the module list on which your instance depends, YUI Loader will dynamically load any missing dependencies; this tends to be a more robust usage, because it can self-complete its requirements. Only use * if you are sure that your instance will never be used in a context in which one or more of its dependencies might be missing.

Module List

Here is a partial list of the modules and their descriptions.

YUI 3 Component Module
Animationanimation
Attribute Engineattribute
Class Builderbase
Collection Utilitiescollection
Drag & Dropdd
DOM Element Utilitesdom
DOM Event Utilitiesevent
Custom Event Engineevent-custome
IO (XHR/XDR)io
JSONjson
JSON parse onlyjson-parse
JSON stringify onlyjson-stringify
Element Wrappernode
Queue Supportqueue

Loader

Loader's functionality is now built into the YUI Global Object (as long as it's on the page) and puts its power behind the YUI.use method.

If you request a module that is not loaded on the page (or a dependency that is not loaded), loader will fetch a copy of that module (and its dependencies) and load them into your YUI instance.

The valid configuration options for Loader are as follows:

  • base: The base dir
  • secureBase: The secure base dir (not implemented)
  • comboBase: The YUI combo service base dir. Ex: http://yui.yahooapis.com/combo?
  • root: The root path to prepend to module names for the combo service. Ex: 2.5.2/build/
  • filter: A filter to apply to result urls. This filter will modify the default path for all modules. The default path for the YUI library is the minified version of the files (e.g., event-min.js). The filter property can be a predefined filter or a custom filter. The valid predefined filters are:
    • DEBUG: Selects the debug versions of the library (e.g., event-debug.js).
    • RAW: Selects the non-minified version of the library (e.g., event.js).
    You can also define a custom filter, which must be an object literal containing a search expression and a replace string:
    myFilter: { 
    'searchExp': "-min\\.js", 
    'replaceStr': "-debug.js"
    }
    
  • combine: Use the YUI combo service to reduce the number of http connections required to load your dependencies
  • ignore: A list of modules that should never be dynamically loaded
  • force: A list of modules that should always be loaded when required, even if already present on the page
  • insertBefore: Node or id for a node that should be used as the insertion point for new nodes
  • charset: charset for dynamic nodes
  • jsAttributes: attributes to apply to dynamic script nodes
  • cssAttributes: attributes to apply to dynamic link nodes
  • timeout: number of milliseconds before a timeout occurs when dynamically loading nodes. in not set, there is no timeout
  • context: execution context for all callbacks
  • modules: A list of module definitions. The valid module configuration data is as follows:
    • name: required, the component name
    • type: required, the component type (js or css)
    • path: required if fullpath is not specified, the path to the script from "base"
    • fullpath: required if path isn't specified, the full path to the script. "base" will not be used to build the url
    • requires: array of modules required by this component
    • optional: array of optional modules for this component
    • supersedes: array of the modules this component replaces
    • after: array of modules the components which, if present, should be sorted above this one
    • rollup: the number of superseded modules required for automatic rollup

Lang

Lang contains javascript language utilities and extensions that are used in the YUI library.

Support & Community

Forums & Blog

YUI 3 discussion forums are hosted on YUILibrary.com.

In addition, please visit the YUIBlog for updates and articles about the YUI Library written by the library's developers.

Filing Bugs & Feature Requests

The YUI Library's public bug tracking and feature request repositories are located on the YUILibrary.com site. Before filing new feature requests or bug reports, please review our reporting guidelines.

Copyright © 2009 Yahoo! Inc. All rights reserved. Copyright | Privacy Policy | Terms of Use | Job Openings