0

即使在阅读了 wiki 和关于何时使用布局与区域等的示例之后,我仍然有点困惑,但我的问题是我无法在页面上显示任何内容 - 根本没有任何渲染。html应该真的只有菜单和内容,内容可以根据Views改变。(我以同样的方式制作菜单,所以我也可以交换它)。

我有一个 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

我已经阅读了文档/示例一百万次,但是所有这些东西的学习曲线都很复杂。请帮忙!

4

1 回答 1

3

你混淆了一些事情,但你几乎在那里

  • ViewOne并且NavBar不应该延长Backbone.View但是Backbone.Marionette.ItemView

  • 您不应该在属性中定义模板,而ViewOne应顾名思义,在属性中定义模板。NavBareltemplate

于 2012-05-16T23:58:21.190 回答