I am trying to access a model method from an .eco template using backbone / marionette.js. I have a model Expensewith a method day()that, using moment.js, returns '13th'; eg:
class Expense extends Backbone.Model
day: ->
moment.get('date').format('Do')
I can create a new one Expenseas follows and call the method day():
coffee = new Expense({name: "Coffee", amount: 2.50, date: "2014-01-13T13:50:00Z"})
coffee.day()
However, trying to access day()from the following view and template causes some problems:
class ExpenseView extends Marionette.ItemView
template: "views/_expense"
<h3 class="expense__name"><%= @name %></h3>
<p class="expense__day"><%= @day() %></p>
I understand why it does not work ... ItemViewcalls serializeDatawhich returns @model.toJSON()... therefore the method is Expense day()not available. Is there an established template in the backbone / puppet network community that allows models to be used for templates?
, :
class ExpenseView extends Marionette.ItemView
template: "views/_expense"
serializeData: ->
_.extend(@model.toJSON(), model: @model)
templateHelpers:
day: ->
@model.day()
, ? !