backbone.marionette区域和布局

时间:2012-07-16 06:13:26

标签: backbone.js javascript-framework marionette

有没有办法为牵线木偶区域指定模板? 现在我正在使用布局对象来指定模板。

 AppLayout = Backbone.Marionette.Layout.extend({

     template: tmpl

    });

    var layout = new AppLayout();
    App.main.show(layout);

    App.addRegions({
        userInfo: "#userInfo",
        mainMenu: "#mainMenu",
        content: "#content"
    });

    App.mainMenu.show(new mainMenuView.Views.menu());
    App.content.show(new dashboard.Views.main());    

为什么我无法直接从我的app对象访问我的区域,当我在布局对象中定义它们时?

 AppLayout = Backbone.Marionette.Layout.extend({

     template: tmpl
     regions: {
         userInfo: "#userInfo",
        mainMenu: "#mainMenu",
        content: "#content"
      }
    });

    var layout = new AppLayout();
    App.main.show(layout);

    does not work:
    App.mainMenu.show(new mainMenuView.Views.menu());
    App.content.show(new dashboard.Views.main());    

由于

2 个答案:

答案 0 :(得分:3)

  

有没有办法为牵线木偶区域指定模板?

这正是一个布局 - 一个渲染模板,在渲染输出中包含区域。

  

为什么我无法直接从我的app对象访问我的区域,当我在布局对象中定义它们时?

布局中的区域范围限定为布局的el,与事件相同。即使您将某个区域定义为“#id”选择器,它仍然限定在布局范围内,并且不会在布局el之外找到任何内容。

此外,在布局上定义区域会将区域添加到布局,而不是应用程序对象。如果要在应用程序对象上定义区域,则必须直接将它们添加到应用程序对象。

答案 1 :(得分:2)

要访问放置在区域中的布局区域,您可以写:

App.main.currentView.mainMenu.show(someView)
App.main.currentView.content.show(anotherView)