Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone Marionette: templateHelpers and itemViewOptions

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.

like image 550
Ingro Avatar asked Aug 21 '12 15:08

Ingro


1 Answers

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

like image 73
evgeniy.klemin Avatar answered Nov 06 '22 03:11

evgeniy.klemin