0

我正在尝试建立一个由 htmls 和 javascript/ajax - jquery 和许多 jquery 插件组成的项目。

对于许多 html 页面中的每一个,我都必须使用 RequireJS,这就是我想要的..

我想根据属性(ajax 调用的返回值)确定我应该加载缩小文件还是非缩小文件

所以我需要一种机制来确定和决定(在页面加载之前或页面开始加载时)我想要加载哪些 js 文件......然后继续加载。

有没有很好的方法来做到这一点?

4

1 回答 1

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>

脚本/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 ...    
    });

  }
});

希望这能让你上路……

于 2011-09-14T17:00:55.623 回答