【问题标题】:Common place to load requirejs config加载requirejs配置的常用地方
【发布时间】:2013-07-31 01:50:39
【问题描述】:

我正在使用 SPA 和 Mvc 3 制作一个混合应用程序。现在我需要的是在骨干网中加载具有条件配置的不同页面。

我已经用这样的方式加载了我的主文件

require
paths:
    jquery:'Libs/jquery/jquery-1.8.0.min'
    underscore:'Libs/Underscore/underscore.min'
    backbone:'Libs/Backbone/backbone-min'
    text:'Libs/Requirejs/text'
    bootstrap:'Libs/Bootstrap/bootstrap.min'
    jqValidation:'Libs/jquery/jquery.validate.min'
    jqValUnobtrusive : 'Libs/jquery/jquery.validate.unobtrusive.min'

shim:
    'underscore':
        exports : '_'
    'backbone':
        deps: ["underscore"]
        exports:'Backbone'
    'bootstrap': 
        deps : ['jquery']
        exports : 'jquery'
    'jqValidation':
        deps : ['jquery']
        exports: 'jQuery.fn.validate'
    'jqValUnobtrusive':
        deps: ['jquery', 'jqValidation']
        exports: 'jQuery.fn.validate.unobtrusive'

require ["App/app","backbone",'bootstrap'] ,(App,Backbone)->
app = new App()
Backbone.history.start()

现在其他文件也需要相同的配置集。现在如何在我需要的每个 main.coffee 文件中加载这个配置?我不希望这个配置是多余的。只是一个地方我想放它然后调用它到其他地方。怎么做 ?

【问题讨论】:

    标签: coffeescript requirejs


    【解决方案1】:

    从单个共享模块加载您的需求配置是一个非常好的计划。

    这是我使用的方法。不同的页面和我的构建系统都使用相同的配置文件。 权衡是你必须嵌套你的 require 依赖项,但这对我来说是值得的。

    Index.html,或任何其他页面

    <script>
        require(['require_config'], function () {
            require(['core'], function (core) {
                window._app = core.start();
            });
        })
    </script>
    

    require_config.coffee

    define [], () ->
      require.config
        packages: [
          'core'
          'dashboard/widgets/base'
        ]
        paths:        
          # 3rd Party Bower Libraries
          handlebars: "bower_components/require-handlebars-plugin/Handlebars"
          underscore: "bower_components/underscore-amd/underscore"
          jqueryui: "bower_components/jquery-ui/jqueryui"
        shim:
          bootstrap:
            deps: [ 'jquery' ]
            exports: 'jquery'
          markdown:
            exports: 'markdown'
    

    【讨论】:

      猜你喜欢
      • 2016-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多