David Fusilier
David Fusilier

Reputation: 301

Marionette CollectionView with one unique ItemView

Is there a way I can create a single unique view within a Marionette CollectionView? I mean something like this:

CollectionView

By unique, I mean the "Start ItemView" must use a unique template, and not have the methods associated with the regular ItemViews.

Things I've tried:

I'm probably missing something fairly obvious. Thanks in advance for the help.

edit: formatting

Upvotes: 2

Views: 1462

Answers (2)

brettjonesdev
brettjonesdev

Reputation: 2281

What you are looking for here is a CompositeView. A CompositeView is a mash-up between an ItemView and a CollectionView. Essentially, the CompositeView itself would act as your StartView, and then you define an ItemView to act as your "Regular ItemView"'s.
Something like this:

RegularItemView = Backbone.Marionette.ItemView.extend({
  template: "#regular-itemview-template"
});

CompositeView = Backbone.Marionette.CompositeView.extend({
  itemView: RegularItemView,
  template: "#start-template"
});

Upvotes: 2

John Mathis
John Mathis

Reputation: 250

Create 2 different views, 'StartView' and 'NormalView':

StartView = Marionette.ItemView.extend({
    template:"#start-template'
});
NormalView = Marionette.ItemView.extend({
    template:"#normal-template"
});

In your collection view, override getItemView and return your 'StartView' and 'NormalView'... something like this:

MyCollectionView = Marionette.CollectionView.extend({
   getItemView:function(item){
       if(item.get("ImTheFirst")===true)
          return StartView;
        else
          return NormalView;
    }
});

On the first item in your collection, add a property 'ImTheFirst' (for example) and set it to true.

Upvotes: 5

Related Questions