引入RequireJS(一)

例子html

http://www.utlcenter.com/user/index.aspxjquery

image

1、文件引用canvas

<script src="Js/require.js" defer async="true" data-main="Js/main.js"></script>app

async属性代表这个文件须要异步加载,避免网页失去响应。IE不支持这个属性,只支持defer,因此把defer也写上。异步

data-main属性的做用是,指定网页程序的主模块async

2、主模块依赖main.js函数

假定主模块依赖jquery、Statistics和GetStatistics这三个模块,main.js就能够这样写requirejs

require.config({

paths: {

"jquery": "jquery-1.9.1.min",

"Statistics": "SiteJs/Statistics",

"GetStatistics": "SiteJs/GetStatistics"

}

});

requirejs(['jquery', 'Statistics', 'GetStatistics'],

function () {

//jQuery, canvas and the app/sub module are all

//loaded and can be used here now.

//var _GetTotalMoney = GetTotalMoney();

//$(".Js_cal").html(_GetTotalMoney);

});

 

3、AMD模块的写法ui

require.js加载的模块,采用AMD规范。也就是说,模块必须按照AMD的规定来写。spa

采用特定的define()函数来定义

4、加载非规范的模块

 

理论上,require.js加载的模块,必须是按照AMD规范、用define()函数定义的模块。可是实际上,虽然已经有一部分流行的函数库(好比jQuery)符合AMD规范,更多的库并不符合。

参考连接

http://www.ruanyifeng.com/blog/2012/11/require_js.html

http://www.requirejs.cn/

http://www.cnblogs.com/snandy/archive/2012/05/22/2513652.html

相关文章
相关标签/搜索