加载requirejs config的常用位置

时间:2012-10-17 06:42:27

标签: coffeescript requirejs

我正在使用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文件中加载此配置?我不希望这个配置是多余的。我想把它放在一个地方,然后把它叫到其他地方。怎么做 ?

1 个答案:

答案 0 :(得分:0)

从单个共享模块加载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'
相关问题