我目前使用的是Marionette 2.4.7,并使用LayoutViews构建了我的应用程序。以非常简单的示例LayoutViews为例(通常模板是分开的):
var LayoutView = Marionette.LayoutView.extend({
template: "<div id="main-region"></div><div id="menu-region"></div>",
regions: {
mainRegion: "#main-region",
menuRegion: "#menu-region"
}
});
var MainLayoutView = Marionette.LayoutView.extend({
template: "<div id="title-region"></div><div id="content-region"></div>",
regions: {
titleRegion: "#title-region",
contentRegion: "#content-region"
}
});基本上,页面分为两部分,一部分用于显示页面标题,另一部分用于显示主要内容。我使用以下内容填充内容区域:
var layoutView = new LayoutView();
var mainLayoutView = new MainLayoutView({ className: "someClass" });
layoutView.mainRegion.show(mainLayoutView);
var contentLayoutView = new ContentLayoutView();
mainLayoutView.contentRegion.show(contentLayoutView);ContentLayoutView有子区域,并做了很多其他的事情,所以是一个相当复杂的视图。MainLayoutView的titleRegion只显示了几个单词,所以非常简单。
现在我的问题是,有没有一种方法可以绕过为titleRegion创建一个ItemView的需要,只是为了显示几个单词?这个场景在我的应用程序中重复了几次,而且需要为每种情况创建单独的ItemViews似乎有些过分了。我知道我可以使用以下命令设置DIV HTML:
$("#title-region").html("Page title");但是我想坚持用Marionette的方式做事情。
我试过了:
mainLayoutView.titleRegion.show("Page name");但我得到的错误是:'view.on' is not a function,显然是因为.show需要一个ItemView/LayoutView。
发布于 2016-08-17 11:01:32
我可能会将标题作为mainLayoutView模板的一部分包含在内。我不确定您使用的是什么模板语言,但它可能看起来像这样:
"<div id="title-region">{title}</div><div id="content-region"></div>"
然后,您可以在MainLayoutView定义中定义templateHelpers函数,如下所示:
templateHelpers: function() {
return { title: this.options.title };
}在创建MainLayoutView时,您可以传入所需的任何标题:
var layoutView = new LayoutView();
var mainLayoutView = new MainLayoutView({
className: "someClass",
title: "Welcome!"
});
layoutView.mainRegion.show(mainLayoutView);
var contentLayoutView = new ContentLayoutView();
mainLayoutView.contentRegion.show(contentLayoutView);title实际上可能已经是视图上的一个属性(我记不清了),所以您可能需要在我使用title的地方使用layoutTitle或类似的东西
https://stackoverflow.com/questions/38986945
复制相似问题