I've a problem with Backbone Marionette and ItemView rendering. I need to pass a value from the Composite View to each of its Item View. The value is contained correctly in the options array of the Item View, however, I cannot access it from the templateHelpers method.
So I tried to set it as value of my View but when I render the array it returns an "undefined" value.
The Composite View
var TableView = Backbone.Marionette.CompositeView.extend({
....
    itemViewOptions: {
        foo: "bar",
    },
The Item View
var RowView = Backbone.Marionette.ItemView.extend({
template: RowTemplate,
tagName: "tr",
foo: "",
initialize: function(){
    this.foo = this.options.foo;              
},
templateHelpers: {  
     foo: function(){
         return this.foo;
     }
},
What I'm doing wrong? How can I access the value and fetch it to the template? Thank you.
More simple solution use construction templateHelpers: function(){return {}}, example:
var RowView = Backbone.Marionette.ItemView.extend({
    // ...
    templateHelpers: function(){  
        return {
            foo: this.foo
        }
    },
    // ...
})
and for using with data:
var RowView = Backbone.Marionette.ItemView.extend({
    // ...
    templateHelpers: function(){
        var foo = this.foo  // context is view
        return {
            something: function(){ 
                return this.name + " " + foo  // context is data object
            }
        }
    },
    // ...
})
Docs: https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.view.md#object-or-function-as-templatehelpers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With