0

I created a table from a composite view, with a model item template:

<script id="vehicle-table-row" type="text/template">
 <td><a href="/vehicles/<%- id %>"><%- id %></a></td>
 <td><%- make %></td>
 <td><%- model %></td>
 <td></td>
</script>

I need to add a value of a field from a different model into the last cell in my row. I haven't found any docs that shows how to do this.

How can I "combine" two models into one template? Is it possible?

vintorg
  • 217
  • 4
  • 17

1 Answers1

1

Using an ItemView you can use serializeData for custom serialization. For instance:

Marionette.ItemView.extend({
  initialize: function(options) {
    this.otherModel = options.otherModel;
  },

  serializeData: function(){
    return _.extend({}, this.model.toJSON(), this.otherModel.toJSON());
  }
});

This will make both your main ItemView model (this.model) and your secondary model (this.otherModel) attributes available in the template. Be careful if the models have attribute names which are the same.

drowzy
  • 657
  • 6
  • 9