我正在尝试建立一个由htmls和javascript / ajax构成的项目 - 带有许多jquery插件的jquery。
对于许多html页面中的每一个,我都必须使用RequireJS,这就是我想要的......
我想基于属性(来自ajax调用的返回值)确定我应该加载缩小文件或非缩小文件
所以我需要一种机制来确定和决定(在页面加载之前或一旦页面开始加载)我想要加载哪些js文件......然后继续加载。
有一个很好的方法吗?
答案 0 :(得分:1)
阱,
看一下这个例子:
html
<!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>
scripts / main.js
$.ajax({
url: "http://server.com/fetchMeSomeData"
context: document.body,
success: function(data){
//this would be your sample callback
// you could be fetching the parameter to ask if it is debug state or not.
var isDebugSuffix = (data.isDebug)? "" : ".min";
require(["helper/util" + isDebugSuffix], function() {
//This function is called when scripts/helper/util.min.js is loaded but when the
//isDebug would be True, it would load scripts/helper/utils.js
//your other code can go here ...
});
}
});
希望这会让你顺利上路......