Reputation: 1473
I am currently developing a web app for a term paper. I only used Angular before but decided that Backbone is definitely worth being given a try. I do, however, have trouble getting the basic skeleton with Require.js running. My code fails when I try to access a Marionette module I required before. Here are the relevant sections of the code:
main.coffee
require.config
# ...
require ["app"], (mod86) ->
mod86.start()
app.coffee
define ["backbone", "marionette", "marionette.handlebars"], (Backbone, Marionette) ->
mod86 = new Backbone.Marionette.Application()
mod86.addRegions
mainRegion: "#main"
mod86.on "initialize:after", ->
require ["index/index"], ->
mod86.mainRegion.show(mod86.Index.Index)
# This is where my code fails, because mod86.Index === undefined
return mod86
index/index.coffee
require ["app", "hbs!/templates/index/index"], (mod86, indexTpl) ->
mod86.module "Index", (Index, mod86, Backbone, Marionette, $, _) ->
Index.Index = Marionette.ItemView.extend
template:
type: "handlebars"
template: indexTpl
serializeData: -> {}
I'm currently reading David Sulc's book on the topic and I thought I followed along pretty closely, I'm really not king anything fancy yet. To me it seems weird to only register the template after the initialization is through, but that's what he did as well to avoid circular dependency. I also tried some different configurations, they always resulted in the same error. I could probably drop the Marionette module system altogether and use only Require, but I feel that's not the way to go.
Any advice would be gladly appreciated!
EDIT:
Adding some console.log
s reveals that the module seems to be created only after I try to access it in my function. After a quick look at the Marionette source I wouldn't know why that should be the case, though.
Upvotes: 0
Views: 96
Reputation: 1473
Alright, I figured it out. In index/index.coffee
I need a define
instead of require
. Appearantly RequireJS doesn't see anything in a require
as eligible as the wanted dependancy, so doesn't execute the callback. After the callback in app.coffee
is through, however, there is a new loaded file that has a require
statement, which is executed then.
So the solution is to always use define
when you require a file, even though you don't return anything at all.
Upvotes: 1