Chaplin.Composer

Why we need it

  • Backbone apps leaked memory hardcore
  • Backbone + Chaplin aggressively garbage collects
  • A facility was needed to allow object persistence

What it does

Grants the ability for views (and related data) to be persisted beyond one controller action.

Regular Controller Action

module.exports = class ServiceController extends Controller
  index: ->
    @collection = new Services
    @view = new ServiceCollectionView {@collection, region: 'main'}
    @collection.fetch().then @view.render

Composed Controller Action

module.exports = class Controller extends Chaplin.Controller
  # Compositions persist stuff between controllers.
  # You may also persist models etc.
  beforeAction: ->
    @reuse 'site', SiteView
    @reuse 'header', HeaderView, {region: 'header'}
    @reuse 'footer', FooterView, {region: 'footer'}

Demo persisted views

Advanced compositions

@reuse 'itemRecommendations',
  compose: ->
    @item = new ItemRecommendations [],
      context: {type: 'Product', scenario: 'Variant Detail'}
    data = {ids: [params.variantId]}
    if isBrowsing
      getRecommendationsCallback @item, data
    else
      unless member.isSynced()
        @listenToOnce member, 'sync', =>
          getRecommendationsCallback @item, _.extend data,
            member_id: member.id
          , options.query
      else
        getRecommendationsCallback @item, _.extend data,
          member_id: member.id
        , options.query

Resources

Made with Slides.com