seajs.configObject
aliasObject
别名配置,配置之后可在模块中使用require调用require('jquery');
2 | alias:
{ 'jquery' : 'jquery/jquery/1.10.1/jquery' } |
4 | define( function (require,
exports,module){ |
6 | var $
=require( 'jquery' ); |
[/code]
pathsObject
设置路径,方便跨目录调用。通过灵活的设置path可以在不影响base的情况下指定到某个目录。
04 | 'gallery' : 'https://a.alipayobjects.com/gallery' |
09 | 'underscore' : 'gallery/underscore' |
12 | define( function (require,
exports,module){ |
13 | var _
=require( 'underscore' ); |
14 | //=>
加载的是https://a.alipayobjects.com/gallery/underscore.js |
varsObject
变量配置。有些场景下,模块路径在运行时才能确定,这时可以使用vars变量来配置。
vars配置的是模块标识中的变量值,在模块标识中用{key}来表示变量。
07 | define( function (require,
exports,module){ |
08 | var lang
=require( './i18n/{locale}.js' ); |
09 | //=>
加载的是path/to/i18n/zh-cn.js |
mapArray
该配置可对模块路径进行映射修改,可用于路径转换、在线调试等。
6 | define( function (require,
exports,module){ |
8 | //=>
加载的是path/to/a-debug.js |
preloadArray
使用preload配置项,可以在普通模块加载前,提前加载并初始化好指定模块。
preload中的空字符串会被忽略掉。
//在老浏览器中,提前加载好ES5和json模块
3 | Function.prototype.bind
? '' : 'es5-safe' , |
注意:preload中的配置,需要等到use时才加载。比如:[/code]
05 | //
在加载b之前,会确保模块a已经加载并执行好 |
12 | define( function (require,
exports){ |
13 | //
此处执行时,不能保证模块a已经加载并执行好 |
debugBoolean
值为true时,加载器不会删除动态插入的script标签。插件也可以根据debug配置,来决策log等信息的输出。
baseString
Sea.js在解析顶级标识时,会相对base路径来解析。
注意:一般请不要配置base路径,把sea.js放在合适的路径往往更简单一致。
charseString|Function
获取模块文件时,<script>或<link>标签的charset属性。默认是utf-8
charset还可以是一个函数:
02 | charset: function (url)
{ |
04 | if (url.indexOf( 'http://example.com/js/xxx' ) ===0){ |
seajs.useFunction
用来在页面中加载一个或多个模块。seajs.use(id,callback?)
05 | seajs.use( './a' , function (a)
{ |
10 | seajs.use([ './a' , './b' ], function (a, b){ |
注意:seajs.use与DOMready事件没有任何关系。如果某些操作要确保在DOMready后执行,需要使用jquery等类库来保证。比如
1 | seajs.use([ 'jquery' , './main' ], function ($, main){ |
2 | $(document).ready( function ()
{ |
seajs.cacheOjbect
通过seajs.cache,可以查阅当前模块系统中的所有模块信息。
比如,打开seajs.org,然后在WebKitDeveloperTools的Console面板中输入seajs.cache,可以看到:
Object
>http://seajs.org/docs/assets/main.js:x
>https://a.alipayobjects.com/jquery/jquery/1.10.1/jquery.js:x
>__proto__:Object
这些就是文档首页用到的模块。展开某一项可以看到模块的具体信息,含义可参考:CMD模块定义规范中的module小节。
seajs.resloveFunction
类似require.resolve,会利用模块系统的内部机制对传入的字符串参数进行路径解析。
[code]1 | seajs.resolve( 'jquery' ); |
2 | // =>http://path/to/jquery.js |
4 | seajs.resolve( './a' , 'http://example.com/to/b.js' ); |
5 | // =>http://example.com/to/a.js |
[/code]
seajs.resolve方法不光可以用来调试路径解析是否正确,还可以用在插件开发环境中。
seajs.dataObject
通过seajs.data,可以查看seajs所有配置以及一些内部变量的值,可用于插件开发。当加载遇到问题时,也可用于调试。
常见问题
关于模块标识
Seajs模块标识主要以小驼峰字符串、.或..
//在http://example.com/js/a.js的factory中:
require.resolve('./b');
//=>http://example.com/js/b.js
//在http://example.com/js/a.js的factory中:
require.resolve('../c');
//=>'target='_blank'>http://example.com/c.js[/code]
分为相对与顶级标识。以.或..开头,则为相对标识
。以小驼峰字符串开关,则为顶级标识。
//假设base路径是:http://example.com/assets/
//在模块代码里:
require.resolve('gallery/jquery/1.9.1/jquery');
//=>'target='_blank'>http://example.com/assets/gallery/jquery/1.9.1/jquery.js[/code]
关于路径
Seajs除了相对与顶级标识之外,还可以使用普通路径来加载模块。
就到当前页面的脚本分析(可以右键查看源码)
//sea.js的路径,即base路径,相对于当前页面
[code]01 | <script
src= "http://yslove.net/actjs/assets/sea-modules/seajs/2.1.1/sj.js" ></script> |
03 | <script
type= "text/javascript" > |
08 | 'actjs' : 'actjs/core/0.0.7/core.js' , |
10 | 'position' : 'actjs/util/0.0.2/position.js' |
17 | 'affix' : '../../actjs/assets/widget/src/widget-affix.js' , |
[/code]