5 分钟上手 Sea.js

jopen 10年前

为什么使用 Sea.js ?

Sea.js 简单、自然的代码书写和组织方式

兼容性好

技术比较成熟,运用比较普遍。

目录结构:

examples/    |-- sea-modules      存放 seajs、jquery 等文件,这也是模块的部署目录    |-- static           存放各个项目的 js、css 文件    |     |-- hello    |     |-- lucky    |     `-- todo    `-- app              存放 html 等文件          |-- hello.html          |-- lucky.html          `-- todo.html

在页面中记载模块:

在 hello.html 页尾,通过 script 引入 sea.js 后,有一段配置代码:

// seajs 的简单配置    seajs.config({    base: "../sea-modules/",    alias: {      "jquery": "jquery/jquery/1.10.1/jquery.js"    }})    // 加载入口模块    seajs.use("../static/hello/src/main")

sea.js 在下载完成后,会自动加载入口模块。

代码模块:

这个小游戏有两个模块 spinning.js 和 main.js,遵循统一的写法:

// 所有模块都通过 define 来定义  define(function(require, exports, module) {      // 通过 require 引入依赖    var $ = require('jquery');    var Spinning = require('./spinning');      // 通过 exports 对外提供接口    exports.doSomething = ...      // 或者通过 module.exports 提供整个接口    module.exports = ...    });

上面就是 Sea.js 推荐的 CMD 模块书写格式。


原文地址:http://seajs.org/docs/#intro