【发布时间】:2014-02-04 07:44:39
【问题描述】:
我正在使用 BrunchJS 来处理咖啡脚本和资产编译。 该项目使用了几个Brunch插件,例如需要“commonjs”包装器才能运行的brunch-handlebar。
从我的 config.coffee
中提取 modules:
# We cant avoid require js wrapping since brunch modules use commonjs
# Otherwise Marionnette JS offers its own modules loading strategy
# loading mechanism
wrapper: "commonjs"
definition: "commonjs"
在 Marionette 方面,我可以很好地加载一个简单的应用程序。
index.html
<script type="text/javascript">
var app = require('application');
app.initialize()
</script>
application.coffee
# 加载车把块助手 需要'lib/view_helper'
class Application extends Backbone.Marionette.Application
initialize: =>
@addInitializer((options) =>
console.log "HELLO WORLD"
AppLayout = require 'views/app_layout'
@layout = new AppLayout()
@layout.render()
)
@start()
# module.exports is the object that's actually returned as the result of
# a require call.
module.exports = new Application()
从那里开始我如何使用 Marionette JS 模块?我在https://github.com/marionettejs/backbone.marionette/wiki/AMD-Modules-vs-Marionette's-Modules 阅读了有关在 AMD 中使用模块的信息,但我不能在我的木偶模块定义中使用 define 关键字,因为“define”和“require”没有公开。 Brunch 只使用它来加载它的插件和我的应用源文件。
一个常见的木偶模块如下所示:
MyApp = new Backbone.Marionette.Application();
MyApp.module("Foo", function(){
// module code goes here
});
MyApp.start();
在一个单独的文件 moduleA.coffee 我尝试这样做:
MyApp = require 'application'
define ["MyApp", "Marionette"], (MyApp, Marionette) ->
MyModule = MyApp.module("MyModule")
MyModule.addInitializer ->
console.log "HELLO FROM MODULE"
MyModule
但是define没有定义。
我也尝试过:
MyApp = require 'application'
MyApp.module "ModuleA", (MyApp, ModuleA, Backbone, Marionette, $, _) ->
ModuleA.addInitializer ->
console.log "HELLO FROM MODULE"
但是我需要在 application.coffee 中要求我所有的木偶模块(“moduleA”)并遇到一些循环依赖问题。
我正在考虑的解决方案之一是禁用 BrunchJS commonjs 包装并从供应商文件夹加载把手,而不是作为早午餐插件。
【问题讨论】:
-
你设置了你的require.config吗?我不确定coffeeScript,但我可以给你一个js例子。
-
不,我没有,因为我相信早午餐是为我们做的?我会试一试的。
-
有可能,早午餐我也不确定。我经常使用require,骨干木偶并且没有这个问题......所以可能值得检查。
标签: requirejs marionette brunch