0
votes

I'm using Marionette with Handlebars templates and I can't get my itemView to render inside a CollectionView.

Here is the CollectionView code:

define( [ 'App', 'marionette', 'handlebars', 'models/Model', 'collections/Collection', 'text!templates/welcome.html'],
function(App, Marionette, Handlebars, Model, Collection, template) {
    //ItemView provides some default rendering logic
    var ItemView = Marionette.ItemView.extend( {
        //Template HTML string
        template: Handlebars.compile(template),
        //model: new Model(),

        // View Event Handlers
        events: {

        },
        initialize: function(o) {
            console.log('init itemView');
        }
    });
    return Marionette.CollectionView.extend( {
        issues: new Collection(),
        itemView: ItemView,
        onRender: function() {this.issues.fetch()},
        initialize: function(o) { console.log('init collectionView')}
    });
});

here is the template

<div class="hero-unit">
<h1>Marionette-Require-Boilerplate Lite</h1>
<p>Lightweight Marionette Boilerplate application to get you off the ground fast.</p>
<p class="muted">
    You are viewing this application on
   </p>
<br/>
<table>
    {{#each items}}
    <tr><td>{{title}} - {{number}}</td></tr>
    {{/each}}
</table>
<a class="btn btn-primary btn-large" href="https:github.com/BoilerplateMVC/">See more Boilerplates</a>

The only thing I get from this code is that the CollectionView does trigger its initialize method and that the collection is fetched from GitHub.

2

2 Answers

2
votes

There are multiple reasons this could not be working, depending on the Marionette version you are using:

  1. For the latest Marionette version, you have to use 'childView' instead of 'itemView'.
  2. The items to display are expected in the property 'collection' not 'issues'.

    example:

      var IssuesView = Marionette.CollectionView.extend({
        childView: IssueView,
        onRender: function () {
          this.collection.fetch();
        },
        initialize: function (o) {
          console.log('init collectionView');
        }
      });
      new IssuesView({'collection': new Backbone.Collection()});
    

Now, based on the code you provided, I assume your goal is to display the issues inside 'items', if that is correct, I will suggest to use a 'CompositeView' instead, and then you can provide a 'container' and render the 'issues' inside the items. For example:

        var IssueView = Marionette.ItemView.extend({
            itemTag: 'tr',
            //Template HTML string
            template: Handlebars.compile($("#item-template").html()),
            //model: new Model(),

            // View Event Handlers
            events: {

            },
            initialize: function (o) {
                console.log('init itemView');
            }
        });
        var IssuesView = Marionette.CompositeView.extend({
            childView: IssueView,
            childViewContainer: "#issues",
            template: Handlebars.compile($("#some-template").html()),
            onRender: function () {
                //this.issues.fetch();
            },
            initialize: function (o) {
                console.log('init collectionView');
            }
        });

Where your templates are:

<script id="item-template" type="text/x-handlebars-template">
   <td> 
    {{title}} - {{number}}
   </td>
</script>
<script id="some-template" type="text/x-handlebars-template">
  <div class = "hero-unit" >
    <h1>Marionette - Require - Boilerplate Lite </h1>
    <p>Lightweight Marionette Boilerplate ...</p>
    <p class = "muted"> You are viewing this application on </p>
    <br/>
    <table id="issues">

    </table>
 </script>

Here is jsfiddle with a working version of this: http://jsfiddle.net/gvazq82/v5yj6hp4/2/

1
votes

Your problem is that you're not specifying a collection in your CollectionView. You want to instead do

var collectionView = Marionette.CollectionView.extend({
    collection: new issues
   ...
});