</pre>app.js 这个是作为requirejs的人口js,即在对应的html页面中有<pre name="code" class="html"><script data-main="js/app" src="js/lib/require.js"></script>
其中app的后缀js可以省略,非AMD格式的使用shim加载,其后为为其依赖项,其代码如下
requirejs.config({
"baseUrl": "js/lib",
"paths": {
"app": "../app"
},
"shim": {
"jquery.alpha": ["jquery"],
"jquery.beta": ["jquery"]
}
});
// Load the main app module to start the app
requirejs(["app/main"]);
main.js 是将由非AMD格式的js文件重新以AMD进行加载,代码如下:
define(["jquery", "jquery.alpha", "jquery.beta"], function($) {
//the jquery.alpha.js and jquery.beta.js plugins have been loaded.
$(function() {
$('body').alpha().beta();
});
});
jquery.alpha.js 加载的js
$.fn.alpha = function() {
return this.append('<p>Alpha is Go!</p>');
};
jquery.beta.js 加载的js
$.fn.beta = function() {
return this.append('<p>Beta is Go!</p>');
};
app.html
<!DOCTYPE html>
<html>
<head>
<title>jQuery+RequireJS Sample Page</title>
<script data-main="js/app" src="js/lib/require.js"></script>
</head>
<body>
<h1>jQuery+RequireJS Sample Page</h1>
<p>Look at source or inspect the DOM to see how it works.</p>
</body>
</html>
目录结构如下图: