[RequireJS]入门目录

很喜欢她官网的风格

§1. 获取RequireJS

§1.1. 就是下载她的js文件

http://www.requirejs.org/docs/download.html

带注释的才82K

§1.2. 还有其他插件,如

文本加载,等待Dom加载完毕,加载CoffeeScript,国际化语言

 

§2. 添加引用

网站结构不变,只是添加RequireJS的js文件即可

HTML代码里,不再需要用<script>标签引用所有的js文件

只需要引用RequireJS一个js文件即可

像下面的代码那样:

<!DOCTYPE html>
<html>
    <head>
        <title>My Sample Project</title>
        <!-- data-main attribute tells require.js to load
             scripts/main.js after require.js loads. -->
        <script data-main="scripts/main" src="scripts/require.js"></script>
    </head>
    <body>
        <h1>My Sample Project</h1>
    </body>
</html>
data-main:入口js代码路径,省略后缀.js
src:RequireJS文件路径

在入口js文件[main.js]里,可以require()方法去加载其他需要的模块

代码如下:

require(["helper/util"], function(util) {
    //This function is called when scripts/helper/util.js is loaded.
    //If util.js calls define(), then this function is not fired until
    //util's dependencies have loaded, and the util argument will hold
    //the module value for "helper/util".
});

这段代码会加载helper/util.js文件

具体用法后面描述

 

§3. 优化

就是把多个文件合并到一个压缩后的文件里去

后面再看

 

§4. 例子

后面再看

posted @ 2014-08-27 14:54  望星辰  阅读(207)  评论(0)    收藏  举报