【问题标题】:Auto init and show view within a region on a Marionette Layout在 Marionette 布局的区域内自动初始化和显示视图
【发布时间】:2013-06-28 19:20:59
【问题描述】:

我有一个布局,有一个区域。当布局初始化时,我希望它自动初始化一个预设视图以进入其区域,并在布局本身显示/关闭时显示/关闭它。

来自https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.layout.md的当前示例:

AppLayout = Backbone.Marionette.Layout.extend({
  template: "#layout-template",    
  regions: {
    mainRegion: "#menu",
    content: "#content"
  }
});

var layout = new AppLayout();
ParentAppLayout.show(layout); // Render the Layout to a parent
layout.mainRegion.show(new SubView());

这个例子表明必须首先显示布局,然后然后我可以初始化并显示子视图。 (上面,如果我在 layout 本身显示之前显示 SubView,则不会发生任何事情,我假设因为选择器在 DOM 中不存在?)

对于可重复使用的布局,我想将此发送视图显示添加到布局本身中,而不是在使用视图的任何地方手动添加它。如何实现?

AppLayout = Backbone.Marionette.Layout.extend({
  template: "#layout-template",    
  regions: {
    mainRegion: "#menu",
    content: "#content"
  },
  initalize: function() {
     this.mainRegion.attachView(new SubView());  
  },
  onShow: function() {
     this.mainRegion.show(this.mainRegion.currentView);
  }
});

var layout = new AppLayout();
ParentAppLayout.show(layout); // Render the Layout to a parent, expecting the child view to also be created automatically

然而,这种方法也没有任何作用 - 没有错误。

【问题讨论】:

    标签: marionette


    【解决方案1】:

    这样做怎么样

    AppLayout = Backbone.Marionette.Layout.extend({
      template: "#layout-template",    
      regions: {
        mainRegion: "#menu",
        content: "#content"
      },
      onShow: function() {
         this.mainRegion.show(new SubView());
      }
    });
    
    var layout = new AppLayout();
    ParentAppLayout.show();
    

    否则,如果创建 SubView 很昂贵,您可以像这样在 initialize 中进行操作

    initialize: function() {
      this.subView = new SubView();
    }
    

    然后在onShow中使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-11
      相关资源
      最近更新 更多