Skip to content

Instantly share code, notes, and snippets.

@ufologist
Created June 20, 2013 07:47
Show Gist options
  • Save ufologist/5820937 to your computer and use it in GitHub Desktop.
Save ufologist/5820937 to your computer and use it in GitHub Desktop.
SeaJS load module mechanism
seajs.use('./js/main', function(main) {
main.hello();
});
<!DOCTYPE html>
<html>
<head>
<title>Hello SeaJS boilerplate</title>
<meta charset="utf-8" />
</head>
<body>
<h1>SeaJS load module mechanism</h1>
<p>Open your console</p>
<ol>
<li>require module: main</li>
<li>require module: mod1</li>
<li>hello mod1</li>
<li>require module: mod2</li>
<li>hello mod2</li>
<li>hello main</li>
</ol>
<h2>Conclusion</h2>
<p>SeaJS execute modules as <strong>LAZY</strong> as possible(SeaJS只会在真正需要使用[依赖]模块时才执行该模块)</p>
<p>SeaJS async load modules, but execute them accordance with the order in your code(SeaJS是异步加载模块的没错, 但执行模块的顺序也是严格按照模块在代码中出现(require)的顺序, 这样才更符合逻辑吧! 你说呢, RequireJS?)</p>
<p>Test with SeaJS 2.0.0</p>
<script data-main="./js/app.js" src="lib/sea.js"></script>
</body>
</html>
define(function(require, exports, module) {
console.log('require module: main');
var mod1 = require('./mod1');
mod1.hello();
var mod2 = require('./mod2');
mod2.hello();
return {
hello: function() {
console.log('hello main');
}
};
});
define(function(require, exports, module) {
console.log('require module: mod1');
return {
hello: function() {
console.log("hello mod1");
}
};
});
define(function(require, exports, module) {
console.log('require module: mod2');
return {
hello: function() {
console.log("hello mod2");
}
};
});
@ufologist
Copy link
Author

SeaJS execute modules as LAZY as possible(SeaJS只会在真正需要使用[依赖]模块时才执行该模块)
SeaJS async load modules, but execute them accordance with the order in your code(SeaJS是异步加载模块的没错, 但执行模块的顺序也是严格按照模块在代码中出现(require)的顺序, 这样才更符合逻辑吧! 你说呢, RequireJS?)
Test with SeaJS 2.0.0

@ufologist
Copy link
Author

执行结果

require module: main
require module: mod1
hello mod1
require module: mod2
hello mod2
hello main

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment