I have a MarionetteJS application that utilizes a CompositeView to show a table of data. What I'd like to do is hide the "Remove" button from all of the ItemViews based on whether a related model is in a specific state. I can do this easily with LayoutView, since in my program flow I can simply put a check like so (where menu is another view with some button(s).
if (ModelIsEditable)
{
myLayout.menuRegion.show(menu);
}
How might I accomplish the same thing with a CompositeView / ItemViews?
My code so far:
CompositeView:
(template)
<script type="text/template" id="data-items-table">
<thead>
<tr>
<th>Title</th>
<th>Number</th>
<th>Default</th>
<th class="no-sort">Actions</th>
</tr>
</thead>
<tbody></tbody>
<tfoot></tfoot>
</script>
(view)
List.DataItems = Marionette.CompositeView.extend({
className: "table table-condensed",
tagName: "table",
template: "#data-items-table",
childView: List.DataItem,
childViewContainer: "tbody"
}
ItemView (template):
<script type="text/template" id="data-item-row">
<td><%= Title%></td>
<td><%= Number%></td>
<td>
<label class="checkbox-inline">
<input type="checkbox" value="<%= IsDefault %>">
</label>
</td>
<td class="js-menu-region">
<a class="btn btn-danger btn-x">
<i class="fa fa-trash-o"></i> Remove
</a>
</td>
</script>
(view)
List.CostCenterFundNumber = Marionette.ItemView.extend({
tagName: "tr",
template: "#data-item-row",
ui: {
"confirm": ".js-behavior-confirmable",
isdefault: "input[type=checkbox]"
},
events: {
"change @ui.isdefault": "toggleDefault"
},
toggleDefault: function () {
if (this.ui.isdefault.prop('checked') == true) {
this.model.set("IsDefault", true);
} else if (this.ui.isdefault.prop('checked') == false) {
this.model.set("IsDefault", false);
}
this.trigger("item:toggleDefault", this.model);
},
onRender: function () {
if (this.model.get('IsDefault')) {
this.ui.isdefault.prop('checked', true);
}
}
});