Search code examples
chaplinjs

ChaplinJS: Referencing a CollectionView property in its template


I have a Collection in ChaplinJS that has the following initialization code:

Chaplin = require 'chaplin'
Collection = require 'models/base/collection'
Domain = require 'models/domain'
mediator = require 'mediator'

module.exports = class Domains extends Collection

  model: Domain

  # Initialize the SyncMachine
  _(@prototype).extend Chaplin.SyncMachine

  initialize: ->
    super
    @.totalHits = 0

How can I reference totalHits in the template of its view? I am using handlebars templates, and writing {{totalHits}} returns nothing.

Incidentally, shouldn't I be able to rewrite the above code with:

module.exports = class Domains extends Collection

  model: Domain
  totalHits: 0

Solution

  • Found the solution:

    In my CollectionView I can override getTemplateData and pass into it whatever I want, including the complete collection object:

      getTemplateData: ->
        templateData = super
        templateData.collection = @.collection
        templateData
    

    Then in the handlebars template I can do {{collection.totalHits}}