2
votes

I am showing a table view using Marionette's composite view. The composite view's template has a tbody with an initial which shows a loadding animation gif.

Now, when the render method is called, I want to remove this initial row and then append the results of collection fetch. However the default render implementation of Marionette seems to be appending to the tbody.

My item template for item view:

<td><input type="checkbox" class="checkboxContact" id="<%-id %>"/></td>
<td><%-name %></td>
<td> <%-msisdn %></td>
<td> <%-email %></td>
<!--
<td> <%-address %></td>
<td> <%-last_modified_time %></td>-->
<td>
  <i rel="tooltip" class="fa fa-pencil-square-o actions"  id="editIcon" title="edit"></i>
  <i rel="tooltip" class="fa fa-trash-o actions" title="delete"></i>
</td>

The overridden render method, as below.

render: function() {
  if (this.collection.size() <= 0) return;

  this.$el.html(this.template((this.collection.toJSON()));
  return this;
}

I also tried

render: function() {
  if (this.collection.size() <= 0) return;

  this.$el.html(this.template({
    collection: this.collection.toJSON()
  }));
  return this;
}

None of these work.

2

2 Answers

2
votes

I don't think you should be overriding your render. Marionette knows how to render things. I'd consider changing how you are implementing the spinner.

You may want to put a spinner onShow and turn it off after the collection syncs https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.compositeview.md#events-and-callbacks

When the collection updates your view will rerender. You can listen to before:render:collection and wipe out the row at that point if you want.

2
votes

If you're using a CompositeView, you can just define:

childViewContainer: "tbody" 

in the compositeview, and the content should be replaced.

Also, when you're using a compositeview, you don't need to call / define the render manually. Just pass the Backbone collection to the view as follows:

var compview = new CompView({ collection: bb_collection });  

Then render it by using a region:

var region = new Marionette.Region({el: "#somediv"}); 
region.show(compview);