I’m still a bit confused even after reading the wiki and examples as to when to use layouts vs regions, etc, but my problem is I can’t get anything to show up on the page – nothing renders at all. The html should really only have a menu and content, and the content can be changed based on Views. (I did the menu the same way so I can swap that as well).
I have an html file:
<!-- 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>
I’ve condensed the js to a single file for this example, 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
The console.log shows:
onShow RegionContainer app.js:19
init App.NavBar app.js:44
init App.ViewOne
init App.MyApp
I’ve read the docs/examples a million times, but the learning curve of all these things is compounded. Please help!
You’re confusing a few thing but you are almost there
ViewOneandNavBarshould not extendBackbone.ViewbutBackbone.Marionette.ItemViewyou should not define the template for
ViewOneandNavBarin theelattribute, but, as the name suggests, in thetemplateattribute.