Sea.js Manual & Documentation


Table of Contents


config

You can use the config function to set the loader configuration options.

seajs.config({
  alias: {
    'es5-safe': 'es5-safe/0.9.2/es5-safe',
    'json': 'json/1.0.1/json',
    'jquery': 'jquery/1.7.1/jquery'
  },
  preload: [
    Function.prototype.bind ? '' : 'es5-safe',
    this.JSON ? '' : 'json'
  ],
  debug: true,
  map: [
    ['http://example.com/js/app/', 'http://localhost/js/app/']
  ],
  base: 'http://example.com/path/to/libs/',
  charset: 'utf-8',
  timeout: 20000
});

The following options are available:

alias

When the module identifier is very long, you can use the alias to simplify code.

seajs.config({
  alias: {
    'app': 'http://path/to/app',
    'jquery': 'jquery/1.7.1/jquery'
  }
});

a.js:

define(function(require, exports, module) {
    var $ = require('jquery');
      //=> http://path/to/libs/jquery/1.7.1/jquery.js

    var biz = require('app/biz');
      //=> http://path/to/app/biz.js
});

If you want to turn off the alias parsing for a specified id, you can add a hash(#) to the beginning of the identifier such as:

define(function(require, exports, module) {
    var $ = require('#jquery');
      //=> http://path/to/libs/jquery.js
});

preload

This configuration can be used to load some modules before any other modules.

// load ES5 safe shim and json in old browsers.
seajs.config({
  preload: [
    Function.prototype.bind ? '' : 'es5-safe',
    this.JSON ? '' : 'json'
  ]
});

Empty string in preload array will be ignored.

debug

If true, the loader will use console.log to print out all warning and error messages. The default is false, and the loader will only throw errors.

In addition, you can set the debug value to 2. In this case, an unique timestamp will be added to each script URL. This can be useful during testing to prevent the browser from using a cached version of the file.

map

This configuration can be used to map one file to another. It's convenient for online debugging. For more information, see the Map Plugin section.

base

The base path that the loader will use when attempting to load top-level module identifiers. For more information, see the Top-Level Identifier section.

** ATTENTION: Please do NOT set the base config unless the base path can not be obtained automatically. See Bootstrapping for more details.

charset

The charset attribute of the <script> elements used to fetch the module files. The default charset is utf-8.

timeout

The number of milliseconds that the loader will wait before determining that a script file is not available. The default is 20000 (20 seconds).

noConflict

To avoid conflicts, or expect to custom the global namespace to your taste, you can use noConflict method to give the control of the seajs variable back to its previous value and return a reference to the loader object.

var myLoader = seajs.noConflict();
myLoader.use('./main');

/* main.js */
define(function(require, exports, module) {
  // snip...
});

If necessary, you can free up the name of the define method as well by passing true as an argument to the method. This is rarely necessary, and if you must do this, please consider carefully.

var myLoader = seajs.noConflict(true);
myLoader.use('./main');

/* main.js */
myLoader.define(function(require, exports, module) {
  // snip...
});