1

在 RequireJS 示例中,它表明您可以从 script 标签引用 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: 'buildlife=@buildlife@'
        });

        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'模块,该模块是与模板内联定义的。如果我对这些文件运行优化器,优化器会正常工作吗,因为模板之一是模块吗?

4

1 回答 1

0

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

我已将模板变量重组为一个 require 全局对象,并在 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>
于 2013-02-07T17:02:12.530 回答