即使在阅读wiki和关于何时使用布局与区域等的示例后,我仍然有点困惑,但我的问题是我无法在页面上显示任何内容 - 根本没有任何渲染。 html应该只有一个菜单和内容,并且内容可以根据视图进行更改。 (我以同样的方式做菜单,所以我也可以交换它。)
我有一个html文件:
<!-- DefaultLayout -->
<script type="text/template" id="template-default">
<div id="region-navbar">
region-navbar
</div>
<div id="region-content">
region-content
</div>
</script>
<!-- NavBar -->
<script type="text/template" id="template-navbar">
<div id="navbar">
my freakin navbar
</div>
</script>
<!-- ViewOne -->
<script type="text/template" id="template-view1">
<div id="view1">
my freakin view
</div>
</script>
<!-- RegionContainer -->
<div id="default-layout-container">
</div>
我已将js浓缩为此示例的单个文件, app.coffee :
window.App = { }
# Region
class RegionContainer extends Backbone.Marionette.Region
el: '#default-layout-container'
# Called on the region when the view has been rendered
onShow: (view) ->
console.log 'onShow RegionContainer'
App.RegionContainer = RegionContainer
# Layout
class DefaultLayout extends Backbone.Marionette.Layout
template: '#template-default'
regions:
navbarRegion: '#region-navbar'
contentRegion: '#region-content'
App.DefaultLayout = DefaultLayout
# NavBar (View)
class NavBar extends Backbone.View
el: '#template-navbar'
initialize: () ->
console.log 'init App.NavBar'
App.NavBar = NavBar
# A View
class ViewOne extends Backbone.View
el: '#template-view1'
initialize: () ->
console.log 'init App.ViewOne'
App.ViewOne = ViewOne
# App
$ ->
# Create application, allow for global access
MyApp = new Backbone.Marionette.Application()
App.MyApp = MyApp
# On application init...
MyApp.addInitializer (data) ->
console.log 'init App.MyApp'
# RegionContainer
regionContainer = new App.RegionContainer
# Layout (holds Views)
defaultLayout = new App.DefaultLayout
regionContainer.show defaultLayout
# Views
navBarView = new App.NavBar
navBarView.render()
viewOne = new App.ViewOne
viewOne.render()
defaultLayout.navbarRegion.show navBarView
defaultLayout.contentRegion.show viewOne
data =
that: 'this'
MyApp.start data
console.log显示:
onShow RegionContainer app.js:19
init App.NavBar app.js:44
init App.ViewOne
init App.MyApp
我已经阅读了一百万次的文档/示例,但所有这些事情的学习曲线更加复杂。请帮忙!
答案 0 :(得分:3)
你混淆了一些事情,但你几乎就在那里
ViewOne
和NavBar
不应延伸Backbone.View
,Backbone.Marionette.ItemView
您不应在ViewOne
属性中定义NavBar
和el
的模板,但顾名思义,在template
属性中。< / p>