开发者

determine which js files to load in html on the fly

I am trying to set up a project which consist of htmls and javascript/ajax - jquery with many jquery plugins.

For each of many html pages, I have to use RequireJS and here is what i want..

I want to determine based on the property(return value from an ajax call) that I should load the minified files or non minified files

So I kind of need a mechanism to determine and decide (before the page load or as soon as the page starts loading) which js files I would want to load... and then go ahead with the load.

Is there a ni开发者_如何学Pythonce way to do this?


well,

take a look at this example:

the 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 ...    
    });

  }
});

hope this set you on your way...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜