0
votes

I have a CompositeView with a list of ItemViews. The same collection is rendered for both states, when a user is logged in, and when a user is logged out.

The ItemView looks roughly like:

<div class="title">  
  {{ title }} 
</div>
{{#if MA.currentUser }}
  Add Review
{{/if}}

With the JavaScript:

MA.Views.Items.Movie = Backbone.Marionette.ItemView.extend({
  template: 'items/movie',
  className: 'movie'
});

However, this does not seem to display the expected 'Add Review'.

Any suggestions what could be done in this situation?

1

1 Answers

0
votes

My understanding is that the template will only have access to the data that is passed to it. By default this is the model which is serialized by serializeData in the View Class which ItemView inherits. You can add additional data by using templateHelpers or writing a custom serializeData(may be other ways also).

Template Helpers

template helpers in a ItemView could be like:

templateHelpers: {
  MA: function(){
    return MA; //Unknown if having the same function name as the global will effect it
  }
}

In your item view. See marionette docs template helper

Template helpers can be a function, object literal or object.

serializeData

For serializeData you just overwrite the serializeData method in your ItemView's Class.

serializeData: function(){
return {
  "some attribute": "some value"
}

Worth noting that this would require you to serialize your model also. marionette docs serializeData

Conclusion

templateHelpers may turn out to be the simplest answer and probably what they were intended for.