我的一些应用程序初始化程序似乎没有触发?

时间:2012-05-16 14:56:35

标签: javascript ruby-on-rails backbone.js coffeescript marionette

我有一个名为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

{{1}}

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

你问:

  

是否无法将一个模块拆分为多个文件?

检查backbone-marionette source确认这正是发生的事情:

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

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