Search code examples
backbone.jscoffeescriptmarionettebackbone-viewsbackbone.js-collections

Template re-renders and doesn't display links in Backbone/Marionette - backbonerails tutorial


I am doing a http://www.backbonerails.com/ tutorial and it's a bit outdated. I am trying to render a headers.jst.eco template and which displays "BackBoneRails Demo" as a header and renders links from Backbone.Collection in list_controller.js.coffee links from getLinksCollection(code below). Instead it re renders the header template several times, doesn't load the links/names and looks like this below in the screenshot

template renders a few times

Here is the source code

app/assets/javascripts/backbone/apps/header/list/templates/_header.jst.eco

<a href="#"><%= @name %></a>

app/assets/javascripts/backbone/apps/header/list/templates/headers.jst.eco

<nav class="navbar navbar-default" id="header">
    <div class="container">
        <div class="pull-left">
            <span class="navbar-brand">BackboneRails Demo</span>
        </div>
        <ul class="nav navbar-nav pull-right">
        </ul>
    </div>

app/assets/javascripts/backbone/apps/header/list/list_view.js.coffee

@Demo.module "HeaderApp.List", (List, App, Backbone, Marionette, $, _) ->

  class List.Header extends Marionette.ItemView
    template: "header/list/templates/_header"
    tagName: "li"

  class List.Headers extends Marionette.CompositeView
    template: "header/list/templates/headers"
    itemView: List.Header
    itemViewContainer: "ul"

    </nav>

app/assets/javascripts/backbone/apps/header/list/list_controller.js.coffee

@Demo.module "HeaderApp.List", (List, App, Backbone, Marionette, $, _) ->

  List.Controller = 

    listHeader: ->
      links = @getLinksCollection()

      headerView = @getHeaderView links
      App.headerRegion.show headerView

    getLinksCollection: ->
      new Backbone.Collection [
        { name: "Link 1"}
        { name: "Link 2"}
        { name: "Link 3"}
        ]

    getHeaderView: (links) ->
      new List.Headers
        collection: links

app/assets/javascripts/backbone/apps/header/header_app.js.coffee

@Demo.module "HeaderApp", (HeaderApp, App, Backbone, Marionette, $, _) -> 
  @startWithParent = false

  API =
    listHeader: ->
      HeaderApp.List.Controller.listHeader()


  HeaderApp.on "start", ->
    API.listHeader()

app/assets/javascripts/backbone/app.js.coffee

@Demo = do (Backbone, Marionette) ->
    App = new Marionette.Application

    App.addRegions
        headerRegion: "#header-region"
        mainRegion: "#main-region"
        footerRegion: "#footer-region"

    App.addInitializer ->
      App.module("HeaderApp").start()
      App.module("FooterApp").start()

    App.on "start", ->
        if Backbone.history
          Backbone.history.start()

    App 

(Didn't include FooterApp code since it works and not related to header template)


Solution

  • Haha, what a coincidence. I bought this series a couple of days ago. Absolute loved it.

    Marionette has had some subtle changes. itemView and ìtemViewContainer are now called childView and childViewContainer:

    List.Headers = App.Views.CompositeView.extend({
        template: 'header/list/templates/headers',
        childView: List.Header,
        childViewContainer: 'ul'
      });
    

    (Sorry for the filthy JavaScript, but I'm more used to that than CoffeScript ;) )