【问题标题】:Some of my application initializers don't seem to trigger?我的一些应用程序初始化程序似乎没有触发?
【发布时间】:2012-05-24 04:09:54
【问题描述】:

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

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

【问题讨论】:

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


【解决方案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;

所以如果你尝试多次定义同一个模块,只会使用第一个定义。

【讨论】:

    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2015-08-23
      • 2013-10-08
      • 2013-01-13
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      相关资源
      最近更新 更多