- seajs.config
- seajs.use
- seajs.cache
- seajs.reslove
- seajs.data
- ³£¼ûÎÊÌâ
- ¹ØÓÚÄ£¿é±êʶ
- ¹ØÓÚ·¾¶
seajs.config
alias
±ðÃûÅäÖã¬ÅäÖÃÖ®ºó¿ÉÔÚÄ£¿éÖÐʹÓÃrequireµ÷Óà require('jquery');
seajs.config({ alias: { 'jquery': 'jquery/jquery/1.10.1/jquery' } }); define(function(require, exports, module) { //ÒýÓÃjQueryÄ£¿é var $ = require('jquery'); });
paths ÉèÖ÷¾¶£¬·½±ã¿çĿ¼µ÷Óá£Í¨¹ýÁé»îµÄÉèÖÃpath¿ÉÒÔÔÚ²»Ó°ÏìbaseµÄÇé¿öÏÂÖ¸¶¨µ½Ä³¸öĿ¼¡£
seajs.config({ //ÉèÖ÷¾¶ paths: { 'gallery': 'https://a.alipayobjects.com/gallery' }, // ÉèÖñðÃû£¬·½±ãµ÷Óà alias: { 'underscore': 'gallery/underscore' } });
vars
±äÁ¿ÅäÖá£ÓÐЩ³¡¾°Ï£¬Ä£¿é·¾¶ÔÚÔËÐÐʱ²ÅÄÜÈ·¶¨£¬Õâʱ¿ÉÒÔʹÓà vars ±äÁ¿À´ÅäÖá£
vars ÅäÖõÄÊÇÄ£¿é±êʶÖеıäÁ¿Öµ£¬ÔÚÄ£¿é±êʶÖÐÓà {key} À´±íʾ±äÁ¿¡£
seajs.config({ // ±äÁ¿ÅäÖà vars: { 'locale': 'zh-cn' } }); define(function(require, exports, module) { var lang = require('./i18n/{locale}.js'); //=> ¼ÓÔصÄÊÇ path/to/i18n/zh-cn.js });
map
¸ÃÅäÖÿɶÔÄ£¿é·¾¶½øÐÐÓ³ÉäÐ޸ģ¬¿ÉÓÃÓÚ·¾¶×ª»»¡¢ÔÚÏßµ÷ÊԵȡ£
seajs.config({ map: [ [ '.js', '-debug.js' ] ] }); define(function(require, exports, module) { var a = require('./a'); //=> ¼ÓÔصÄÊÇ path/to/a-debug.js });
preload
ʹÓÃpreloadÅäÖÃÏ¿ÉÒÔÔÚÆÕͨģ¿é¼ÓÔØÇ°£¬ÌáÇ°¼ÓÔز¢³õʼ»¯ºÃÖ¸¶¨Ä£¿é¡£
preloadÖеĿÕ×Ö·û´®»á±»ºöÂÔµô¡£
// ÔÚÀÏä¯ÀÀÆ÷ÖУ¬ÌáÇ°¼ÓÔغà ES5 ºÍ json Ä£¿é seajs.config({ preload: [ Function.prototype.bind ? '' : 'es5-safe', this.JSON ? '' : 'json' ] });
×¢Ò⣺preloadÖеÄÅäÖã¬ÐèÒªµÈµ½ use ʱ²Å¼ÓÔØ¡£±ÈÈ磺
seajs.config({ preload: 'a' }); // ÔÚ¼ÓÔØ b ֮ǰ£¬»áÈ·±£Ä£¿é a ÒѾ¼ÓÔز¢Ö´Ðкà seajs.use('./b');
debug
ֵΪtrueʱ£¬¼ÓÔØÆ÷²»»áɾ³ý¶¯Ì¬²åÈëµÄ script ±êÇ©¡£²å¼þÒ²¿ÉÒÔ¸ù¾ÝdebugÅäÖã¬À´¾ö²ß log µÈÐÅÏ¢µÄÊä³ö¡£
base
Sea.js ÔÚ½âÎö¶¥¼¶±êʶʱ£¬»áÏà¶Ô base ·¾¶À´½âÎö¡£
×¢Ò⣺һ°ãÇë²»ÒªÅäÖà base ·¾¶£¬°Ñ sea.js ·ÅÔÚºÏÊʵÄ·¾¶ÍùÍù¸ü¼òµ¥Ò»Ö¡£
charse
»ñÈ¡Ä£¿éÎļþʱ£¬<script> »ò <link> ±êÇ©µÄcharsetÊôÐÔ¡£ ĬÈÏÊÇutf-8 charset»¹¿ÉÒÔÊÇÒ»¸öº¯Êý£º
seajs.config({ charset: function(url) { // xxx Ŀ¼ÏµÄÎļþÓà gbk ±àÂë¼ÓÔØ if (url.indexOf('http://example.com/js/xxx') === 0) { return 'gbk'; } // ÆäËûÎļþÓà utf-8 ±àÂë return 'utf-8'; } });
seajs.use
ÓÃÀ´ÔÚÒ³ÃæÖмÓÔØÒ»¸ö»ò¶à¸öÄ£¿é¡£seajs.use(id, callback?)
// ¼ÓÔØÒ»¸öÄ£¿é seajs.use('./a'); // ¼ÓÔØÒ»¸öÄ£¿é£¬ÔÚ¼ÓÔØÍê³Éʱ£¬Ö´Ðлص÷ seajs.use('./a', function(a) { a.doSomething(); }); // ¼ÓÔضà¸öÄ£¿é£¬ÔÚ¼ÓÔØÍê³Éʱ£¬Ö´Ðлص÷ seajs.use(['./a', './b'], function(a, b) { a.doSomething(); b.doSomething(); });
×¢Ò⣺seajs.use Óë DOM ready ʼþûÓÐÈκιØϵ¡£Èç¹ûijЩ²Ù×÷Ҫȷ±£ÔÚ DOM ready ºóÖ´ÐУ¬ÐèҪʹÓà jquery µÈÀà¿âÀ´±£Ö¤¡£±ÈÈç
seajs.use(['jquery', './main'], function($, main) { $(document).ready(function() { main.init(); }); });
×¢Ò⣺use·½·¨µÚÒ»¸ö²ÎÊýÒ»¶¨ÒªÓУ¬µ«ÊÇ¿ÉÒÔÊÇnull£¬Ò²¿ÉÒÔÊÇÒ»¸ö±äÁ¿
var bootstrap = ['bootstrap.css', 'bootstrap-responsive.css', 'bootstrap.js']; seajs.use(bootstrap, function() { //do something });
seajs.cache
ͨ¹ý seajs.cache£¬¿ÉÒÔ²éÔĵ±Ç°Ä£¿éϵͳÖеÄËùÓÐÄ£¿éÐÅÏ¢¡£
±ÈÈ磬´ò¿ª seajs.org£¬È»ºóÔÚ WebKit Developer Tools µÄ 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.reslove
ÀàËÆrequire.resolve£¬»áÀûÓÃÄ£¿éϵͳµÄÄÚ²¿»úÖƶԴ«ÈëµÄ×Ö·û´®²ÎÊý½øÐз¾¶½âÎö¡£
seajs.resolve('jquery'); // => http://path/to/jquery.js seajs.resolve('./a', 'http://example.com/to/b.js'); // => http://example.com/to/a.js
seajs.resolve ·½·¨²»¹â¿ÉÒÔÓÃÀ´µ÷ÊÔ·¾¶½âÎöÊÇ·ñÕýÈ·£¬»¹¿ÉÒÔÓÃÔÚ²å¼þ¿ª·¢»·¾³ÖС£
seajs.data
ͨ¹ý 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'); // => http://example.com/c.js
·ÖΪ Ïà¶Ô Óë ¶¥¼¶ ±êʶ¡£ÒÔ.»ò..¿ªÍ·£¬ÔòΪÏà¶Ô±êʶ ¡£ÒÔСÍÕ·å×Ö·û´®¿ª¹Ø£¬ÔòΪ¶¥¼¶±êʶ¡£
// ¼ÙÉè base ·¾¶ÊÇ£ºhttp://www.aseoe.com/assets/
// ÔÚÄ£¿é´úÂëÀ require.resolve('gallery/jquery/1.9.1/jquery'); // => http://www.aseoe.com/assets/gallery/jquery/1.9.1/jquery.js
//sea.jsµÄ·¾¶£¬¼´ base ·¾¶£¬Ïà¶ÔÓÚµ±Ç°Ò³Ãæ <script src="../actjs/assets/sea-modules/seajs/2.1.1/sj.js"></script> <script type="text/javascript"> //ÅäÖÃSeajs seajs.config({ alias: { //¶¥¼¶±êʶ£¬»ùÓÚ base ·¾¶ 'actjs': 'actjs/core/0.0.7/core.js', // => http:// 'position': 'actjs/util/0.0.2/position.js' } }); seajs.config({ alias: { //ÆÕͨ·¾¶£¬Ïà¶ÔÓÚµ±Ç°Ò³Ãæ 'affix': '../../actjs/assets/widget/src/widget-affix.js', //Ïà¶Ô±êʶ£¬Ïà¶ÔÓÚµ±Ç°Ò³Ãæ 'init': './src/init.js' } }); </script>
¹ØÓÚ·¾¶
Seajs³ýÁËÏà¶ÔÓ붥¼¶±êʶ֮Í⣬»¹¿ÉÒÔʹÓÃÆÕͨ·¾¶À´¼ÓÔØÄ£¿é¡£
¾Íµ½µ±Ç°Ò³ÃæµÄ½Å±¾·ÖÎö
¿ªÊ¼µÄʱºò»á¾õµÃSeajsµÄ·¾¶Óе㲻ϰ¹ß£¬ÓÉÆäÊÇBase·¾¶¡£ÇмÇBase·¾¶¾ÍÊÇsea.jsµÄÄǸöÎļþµÄÉϼ¶Â·¾¶£¬È»ºóËùÓж¥¼¶±êʶ£¬Ïà¶Ô±êʶ¶¼ÊÇÏà¶ÔÓÚÕâ¸öBaseÀ´µ÷Õû¡£
תÔØÇë×¢Ã÷£º ÎÄÕÂתÔØ×Ô£º°®Ë¼×ÊÔ´Íø http://www.aseoe.com/show-12-592-1.html