1

I am a beginner in Marionette and trying to get a view output to be like this:

<div class="style_title">Component Library</div>
<ul class="style_content">
    <li class="style_item">title1</li>
    <li class="style_item">title2</li>
</ul>

This is ItemView and CollectionView:

var TitleView = Marionette.ItemView.extend({
       template: _.template("<%=title%>"),
       tagName: "li",
       className: "style_item"
    });

    var TitleListView = Marionette.CollectionView.extend({
        tagName: "ul", 
        className: "style_content",
        initialize: function() {
            this.collection = new Backbone.Collection();
            ComponentService.getComponents().forEach(function (title) {
                this.collection.add(title);
            }.bind(this));
        },
        childView: TitleView  
    });

How to add div attribute to get a needed output. There is a possibility to use text!js, but I can't get things together. Thank you in advance.

I.N
  • 45
  • 1
  • 6
  • what do you mean by `add div attribute` – Sami Sep 24 '15 at 10:44
  • I mean this div element(
    Component Library
    ) before CollectionView sequence, so that output be
    – I.N Sep 24 '15 at 10:45
  • what `sequence`? Can you elaborate on that? – Sami Sep 24 '15 at 10:48
  • What should I add or remove form CollecionView to get an html output like this
    Component Library
    • title1
    • title2
    – I.N Sep 24 '15 at 10:52
  • And you don't want to use ` – Sami Sep 24 '15 at 10:53
  • This template is situated not in this file. How could i "tell" js to search for "#some_tag"? For example, my views are in app/views and templates are in app/templates. – I.N Sep 24 '15 at 10:56
  • Well to do so you need to link your javascript file in the html template like `` else you can use `requirejs` and use [requirejs text](https://github.com/requirejs/text) – Sami Sep 24 '15 at 11:05
  • might be a useful [answer](http://stackoverflow.com/questions/14097535/loading-templates-with-backbone-js) – Sami Sep 24 '15 at 11:15

1 Answers1

0

You can use CompositeView instead of Collection view in which you can pass template not just set a tag and specify container for items.

var TitleListView = Marionette.CompositeView.extend({
    template: _.template('<div class="style_title">Component Library</div><ul class="style_content"></ul>'),
    childViewContainer: 'ul.style_content',
    initialize: function() {
        this.collection = new Backbone.Collection();
        ComponentService.getComponents().forEach(function (title) {
            this.collection.add(title);
        }.bind(this));
    },
    childView: TitleView
});

Actually in the next versions of Marionette they plan to remove CompositeView and allow to pass template to CollectionView.

antejan
  • 2,594
  • 12
  • 15
  • It can't find childViewContainer. Is it value is taken from template? – I.N Sep 24 '15 at 11:30
  • Sorry, that was my mistake. That's exacly what I want. It's by default adding collectionView in div. Is it ok?
    What_I_Want
    – I.N Sep 24 '15 at 11:38
  • As stated in docs in childViewContainer option you can specify a jQuery selector to put the `childView` instances into. So yes, you can put your items into any element inside your template you want. – antejan Sep 24 '15 at 11:41