可以使用 config 方法来配置加载器。
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
});
支持以下配置选项:
当模块标识很长时,可以使用 alias 配置来简化。
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
});
解析某个模块标识时,如果不想解析别名,可以在标识前面添加一个井号(#):
define(function(require, exports, module) {
var $ = require('#jquery');
//=> http://path/to/libs/jquery.js
});
使用 preload 配置项,可以在普通模块加载前,提前加载并初始化好特定模块。
// 在老浏览器中,提前加载好 ES5 和 json 模块:
seajs.config({
preload: [
Function.prototype.bind ? '' : 'es5-safe',
this.JSON ? '' : 'json'
]
});
preload 中的空字符串会被忽略掉。
值为 true 时,加载器会使用 console.log 输出所有警告和错误。
默认为 false, 加载器只抛出异常。
另外,还可以将 debug 值设为 2 . 这种情况下,
每个脚本请求都会加上唯一时间戳。这在测试期间很有用,可以强制浏览器每次都请求最新版本。
该配置可将某个文件映射到另一个。可用于在线调试,非常方便。更多信息,请参考 映射插件。
SeaJS 在解析顶级标识时,会相对 base 路径来解析。详情请参阅
顶级标识。
** 注意:请不要配置 base 路径,除非加载器无法自动获取。详情请参考 加载方式。
获取模块文件时,<script> 标签的 charset 属性。
默认是 utf-8。
加载器等待脚本加载的最长时间。单位为毫秒,默认值是 20000(20秒)。
为了避免冲突,或者需要定制全局命名空间以符合自己的口味时,可以使用
noConflict 方法来实现。
var myLoader = seajs.noConflict();
myLoader.use('./main');
/* main.js */
define(function(require, exports, module) {
// snip...
});
还可以通过给该方法传递 true,来释放 define 方法。 很少会有这么做的必要,
请三思而后行。
var myLoader = seajs.noConflict(true);
myLoader.use('./main');
/* main.js */
myLoader.define(function(require, exports, module) {
// snip...
});