我使用BrunchJS来处理咖啡脚本和资产编译。 该项目使用多个Brunch插件,如需要“commonjs”包装器操作的brunch-handlebar。从我config.coffeeMarionette JS模块,Brunch JS和需要JS项目结构
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"
在木偶侧
提取物,我可以有一个简单的应用程序加载就好了。
的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模块?我在这里阅读了关于在这里使用AMD的模块https://github.com/marionettejs/backbone.marionette/wiki/AMD-Modules-vs-Marionette的模块,但由于“定义”和“要求”没有公开,所以我不能在我的木偶模块定义中使用定义关键字。 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
但定义没有定义。
我也试图做的事:
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例子。 –
不,因为我相信早午餐是为我们做的吗?我会试一试。 – coulix
这是可能的,我也不确定早午餐。我使用require,骨干牵线木偶,并没有这个问题...所以可能值得检查。 –