2013-02-04 28 views
1

在RequireJS例子,它表明你可以引用app.js(或任何你要拨打的启动文件),从脚本标签,像这样:RequireJS优化器是否在没有“app.js”和依赖于内联模块的模块上工作?

<script data-main="js/app.js" src="js/require.js"></script> 

对于超出了我的控制,我可以考虑不要这样做。在模板层中有几个动态变量需要保留。所以,我创建了一个其他模块可以读取的内联“配置”模块。

<script type="text/javascript"> 
     define('config', function() { 
      return { 
       markup_id: { 
        "content": "search", 
        "page": "index", 
        "media": "mobile" 
       }, 
       page_context: { 
        "siteconfig": { 
         "mobile_video_player_id": /* */, 
         "mobile_video_player_key": /* */, 
         "mobile_ad_site": /* */, 
         "omniture_mobile_env": /* */, 
         "searchserver": /* */,    
        }, 
        "omniture": { 
         "gn": /* */, 

        } 
       } 
      } 
     }); 
    </script>  

我所做的是针对每个模板,我放置了一个内联的require.config。作为一个例子(删除特定路径信息):

<script type="text/javascript"> 
/* This code is on a template page inside a script tag. */ 
require.config({ 
      baseUrl: /* */, 
      paths: { 
       'jquery': /* */, 
       'jquery-mobilead': /* */, 
       'jquery-photogalleryswipe': /* */ 
      }, 
      /* Enforce ordering of jQuery plugins - which require jquery */ 
      shim: { 
       'jquery-mobilead': { 
        deps: ['jquery'], 
        exports: 'jQuery.fn.mobileAd' 
       }, 
       'jquery-photogalleryswipe': { 
        deps: ['jquery'], 
        exports: 'jQuery.fn.photoGallerySwipe' 
       }, 
       'gallery': { 
        deps: ['jquery-photogalleryswipe', 'jquery-mobilead'] 
       } 
      }, 
      urlArgs: '[email protected]@' 
     }); 

     require(['jquery', 'site', 'gallery', 'jquery-photogalleryswipe', 'jquery-mobilead'], function($, site, gallery) { 
       //This function will be called when all the dependencies 
       //listed above are loaded. Note that this function could 
       //be called before the page is loaded. 
       //This callback is optional. 

       /* Initialize code */ 
       $(document).ready(function() { 
        /* sitewide code - call the constructor to initialize */ 
        site.init(); 
        /* homepage contains a reference to a function - execute the function */ 
        gallery.initGallery(); 
       }); 
      } 
     ); 
</script> 

我认为优化器无法优化模板内的代码。

但是根据RequireJS API文档,我确实有模块JS文件。
/modules/gallery.js /modules/channel.js /modules/site.js /*等*/

这些模块做有依赖于其他模块,但是这些模块都依赖于“ config'模块,它与模板内嵌定义。如果我针对这些文件运行Optimizer,那么优化器是否能够正常工作,因为其中一个模块config,in是模板?

回答

0

我想我解决我自己的问题基于阅读 How to load bootstrapped models in Backbone.js while using AMD (require.js)

我已经重新调整了模板变量为需要全局对象和require.js之前声明它。现在,模板仍然可以生成这些值,但由于require.config和require代码可以放入外部JS文件中,因此优化器应该能够立即查看这些文件。我还没有尝试过运行优化器。

<script> 
     /* This script block is in the template */ 
     var require = { 
      config: { 
       markup_id: { 
       "content": "search", 
       "page": "index", 
       "media": "mobile" 
       }, 
       page_context: { 
        "siteconfig": { 
        "mobile_video_player_id": /* */, 
        "mobile_video_player_key": /* */, 
        "mobile_ad_site": /* */, 
        "omniture_mobile_env": /* */, 
        "searchserver": /* */,    
        }, 
       "omniture": { 
        "gn": /* */, 

       } 
      } 
     }; 
    </script> 
<script data-main="/m/j/main-search" src="/m/j/require.js"></script>