Search code examples
backbone.jsmarionette

Error : CollectionView require ItemView


My example :

var stooges = [{ name: 'moe', age: 44, userid: 1}, 
               { name: 'larry', age: 44, userid: 2}, 
               { name: 'curly', age: 44, userid: 3}];





var StoogeModel = Backbone.Model.extend({});


var StoogeCollection = Backbone.Collection.extend({
  model: StoogeModel
});



var StoogeItemView = Backbone.Marionette.ItemView.extend({
  tagName: "tr",
  template: '#stooge-template'
});

var StoogesCollectionView = Backbone.Marionette.CollectionView.extend({
  tagName: "table",
  childView: StoogeItemView
});






var myStooges = new StoogeCollection(stooges);

var myStoogesView = new StoogesCollectionView({ collection: myStooges  });


myStoogesView.render();

document.body.appendChild(myStoogesView.el);

This example I read in topic backbone.js collection view example using marionette template but I have error:

marionette_backbone.js:1299 Uncaught NoItemViewError: An itemView must be specified

Help me please.


Solution

  • You're using Marionette 1.x as a dependency in your project, but you're attempting to use 2.x interfaces. In 1.x CollectionViews used an "itemView", while 2.x changed the naming to "childView"

    Changing your StoogesCollectionView definition to use the itemView naming should fix your issue:

    var StoogesCollectionView = Backbone.Marionette.CollectionView.extend({
      tagName: "table",
      itemView: StoogeItemView
    });
    

    Alternatively, you can upgrade Marionette to a newer version.