gpt4 book ai didi

marionette - 在 Marionette 布局上的区域内自动初始化和显示 View

转载 作者:行者123 更新时间:2023-12-01 23:24:26 27 4
gpt4 key购买 nike

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

当前示例来自 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 本身显示之前显示 SubView,什么也不会发生,我假设是因为 DOM 中不存在选择器?)

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

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

然而,这种方法也没有做任何事情 - 没有错误。

最佳答案

这个怎么样

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 中使用它。

关于marionette - 在 Marionette 布局上的区域内自动初始化和显示 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17363344/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com