Some of my application initializers don't seem to trigger?

Question!

I have a top level application called SearchApp which has a sub-app called TeamApp. The files are structured like this:

search_app.js.coffee # The top-level application.
team_app/
  app.js.coffee
  team_list.js.coffee
  team_invite.js.coffee

I initialize my application in search_app.js.coffee:

window.Domainer = {}

# This is the top level application.
Domainer.SearchApp = new Backbone.Marionette.Application()

# Assign a region to the Application.
Domainer.SearchApp.addRegions(stage: '#stage')

And start it in the html view:

<script>Domainer.SearchApp.start({});</script>

The Submodule TeamApp is laid out over a few files (below). THe problem is that some of the files in the TeamApp module don't seem to be able to add initializers to the SearchApp. This is evidenced by the fact that I can console.log from the initialization in one file but not in the other.

# team_app/app.js.coffee
Domainer.SearchApp.module "TeamApp", (TeamApp, SearchApp, Backbone, Marionette, $, _) ->

  # Initializers
  # ----------

  SearchApp.addInitializer (options) ->
    console.log "This will log when I call Domainer.SearchApp.start()"

  # In coffeescript it's important to explicitly return.
  return TeamApp

# team_app/team_list.js.coffee
Domainer.SearchApp.module "TeamApp", (TeamApp, SearchApp, Backbone, Marionette, $, _) ->

  class CompactSearcher extends Marionette.ItemView
    # ... various code relating to this view.

  class TeamList extends Marionette.CollectionView
    # various code relating to this view.

  SearchApp.addInitializer (options) ->
    console.log "This will never log for some reason."

  return TeamApp

# team_app/invite_view.js.coffee
Domainer.SearchApp.module "TeamApp", (TeamApp, SearchApp, Backbone, Marionette, $, _) ->

  class InviteView extends Marionette.ItemView
    # ... various code relating to this view.

  SearchApp.addInitializer (options) ->
    console.log "This will never log either."

  return TeamApp

Is it not possible to split one module across multiple files? That's the only thing that I can think is happening here. What else could be causing the problem?



Answers

You ask:

Is it not possible to split one module across multiple files?

Checking the backbone-marionette source confirms that this is exactly what's going on:

// Get the module name, and check if it exists on
// the current parent already
moduleName = moduleNames[i];
module = parentModule[moduleName];

if (!module){ 
  // This is where the module definition is used...
}

return module;

So if you try to define the same module multiple times, only the first definition will be used.



This video can help you solving your question :)
By: admin