视图无渲染,区域与布局

时间:2012-05-16 22:35:18

标签: javascript backbone.js coffeescript marionette

即使在阅读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

我已经阅读了一百万次的文档/示例,但所有这些事情的学习曲线更加复杂。请帮忙!

1 个答案:

答案 0 :(得分:3)

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

  • ViewOneNavBar不应延伸Backbone.ViewBackbone.Marionette.ItemView

  • 您不应在ViewOne属性中定义NavBarel的模板,但顾名思义,在template属性中。< / p>