How to access the Backbone.Model methods from the ItemView template from Marionette.js?

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() # 13th

However, trying to access day()from the following view and template causes some problems:

class ExpenseView extends Marionette.ItemView
  template: "views/_expense"
# views/_expense.jst.eco
<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()

, ? !

+4
2

templateHelpers serializeData - , , - - , , , ViewModel. sync , save()

, , - Backbone Mutators - https://github.com/asciidisco/Backbone.Mutators

, Backbone Computed Fields - https://github.com/alexanderbeletsky/backbone-computedfields, Ember .

Backbone Mutators Backbone.

class Model extends Backbone.Model
  mutators:
    day: ->
      moment.get('date').format('Do')

, "day" ...

class Model extends Backbone.Model
  mutators:
    day:
      get: -> moment.get('date').format('Do')
      transient: true

day toJSON().

+7

serializeData? ( ...)

serializeData: function(){
    var data = _.clone(this.model.attributes);
    data.day = this.model.day();
    return data;
}
0

All Articles