Search code examples
javascriptjquerybackbone.jsmarionette

is it possible to avoid adding default view element "div" when using appendHtml() in Marionette?


I am appending manually the view to a dom element in my template with the following code:

appendHtml: function(collectionView, itemView, index){
      collectionView.$("ul#search_list_container").append(itemView.el);
}

In my template I have:

<script id='search-list-grid-template' type='text/x-handlebars-template'>
    <ul id="search_list_container"></ul>
</script>

Despite I am appending the view to the ul#search_list_container I have the default div wrapping the template:

<div>
    <ul id="search_list_container">
    <a href="#">
        <span id="search_list_item">
            id
            invoice_number
        </span>
    </a>
    </li>
    </ul>
</div>

is there a way to avoid displaying the default tag "div"?, I have no problem with this but this doubt has always come to my mind whenever I come up with this example.

Note: I have an itemView for the ul compositeView, and some other stuff not shown here.


Solution

  • Backbone Views are designed to have their own DOM element stored as the view's el property.

    It is not recommended to remove the view's element as suggested by Steven-Farley, because events might be bound to it.

    The best way would be to change the tagName property of your collectionView to ul.

    See also: Extra divs in itemviews and layouts in Backbone.Marionette