我有一个布局,有一个区域。当布局初始化时,我希望它自动初始化一个预设视图以进入其区域,并在布局本身显示/关闭时显示/关闭它。
当前示例来自https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.layout.md 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());
这个例子表明必须首先显示布局,之后然后我可以初始化并显示子视图。 (上面,如果我显示SubView
之前layout
本身已显示,什么也不会发生,我假设是因为选择器不存在于 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
然而,这种方法也没有做任何事情——没有错误。