0

我有一个名为的顶级应用程序SearchApp,它有一个名为TeamApp. 这些文件的结构如下:

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

我在以下位置初始化我的应用程序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')

并在 html 视图中启动它:

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

子模块TeamApp分布在几个文件中(如下)。问题是TeamApp模块中的某些文件似乎无法将初始化程序添加到SearchApp. 事实证明,我可以console.log在一个文件中进行初始化,但不能在另一个文件中进行初始化。

# 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

不能将一个模块拆分为多个文件吗?这是我能想到的唯一事情发生在这里。还有什么可能导致问题?

4

2 回答 2

2

仅供参考 - 我现在更新了 Marionette 以支持这种情况。



MyApp = new Backbone.Marionette.Application();

MyApp.module("Foo", function(Foo){
  Foo.def1 = true;
});

MyApp.module("Foo", function(Foo){
  Foo.def2 = true;
});

MyApp.Foo.def1; //=> true
MyApp.Foo.def2; //=> true
于 2012-05-17T20:39:02.893 回答
1

你问:

不能将一个模块拆分为多个文件吗?

检查骨干木偶源确认这正是正在发生的事情:

// 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;

因此,如果您尝试多次定义同一个模块,则只会使用第一个定义。

于 2012-05-16T15:39:04.307 回答