利用data.main或require来使用require.js?

时间:2014-09-12 01:21:06

标签: javascript requirejs

我的头脑中有以下几行。

<script data-main="{% static 'site_common/js/main.js' %}"  src='{% static "site_common/bower_components/requirejs/require.js" %}'></script>

main.js有

require(['site_common/js/config'], function() {
});

在身体的底部,我有

 require(["site_common/js/config"], function () {

   require(['infrastructure'], function () {

   require([
       'content_tab/apps/content-tab-app',
     ], function(ContentTabApp) {

       var appOptions = {
         el: "#ann-content-app",
         contenttype: contenttype,
         threads_obj: threads_obj,
         thread_obj: thread_obj
       };

       var content_tab_App = new ContentTabApp(appOptions);

       Backbone.history.start({
         pushState: true,
         hashChange: false
       });


     });
   });

 });

我有第一行(使用data-main)因为我认为它是必需的,但现在我认为它是超级的。

但是如果我删除该行,该页面如何知道它需要自己下载require.js?

1 个答案:

答案 0 :(得分:0)

你正在走上正轨。在这种情况下,您要尝试的是加载require.js脚本并传递指向应用程序入口点的data-main属性main.js

但是,您还应该看看其他几种模式at

<script data-main="{% static 'site_common/js/main.js' %}"  src='{% static "site_common/bower_components/requirejs/require.js" %}'></script>

...然后在main.js

require(["site_common/js/config"], // dependencies 1st loaded
function () { // callback after dependencies above have loaded successfully

   require(['infrastructure'], // this loads 2nd
   function () { // after second nested dependency loaded this callback will be executed

   require(['content_tab/apps/content-tab-app'], // this loads 3rd
    function(ContentTabApp) { // after third dependency has loaded this function will be called

       var appOptions = {
         el: "#ann-content-app",
         contenttype: contenttype,
         threads_obj: threads_obj,
         thread_obj: thread_obj
       };

       var content_tab_App = new ContentTabApp(appOptions);

       Backbone.history.start({
         pushState: true,
         hashChange: false
       });


     });
   });

 });