Monthly Archives: March 2014

Project Technologies Setup:

Screen Shot 2014-03-12 at 10.17.02

JS: CoffeeScript – ChaplinJS – BackboneJS – jQuery – Lodash – RequireJS
HTML: Handlebars
CSS: SASS – Compass


Why the heck can’t I store the user in Chaplin.mediator.user?


"Uncaught TypeError: Can't add property user, object is not extensible"

Googling I get this: Object.preventExtensions() but couldn’t find any reference to this method in Chaplin’s code. But I do see a method called


What’s this? –> Google:

Attempting to add new properties to a non-extensible object will fail

Well… ok but I would have thought that Chaplin has already the user property set. No, it isn’t set.

I was following the Chaplin project example of:

And there I found the piece of code, inside

initMediator: ->
# Create a user property
Chaplin.mediator.user = null
# Seal the mediator

Chaplin seems to be routing twice – Controllers actions seems to be called twice when clicked on links
Navigation setup:

  <li><a class="header-link new" href="new">New</a></li>
  <li><a class="header-link approved" href="approved">Approved</a></li>


No listener is attached to a to control it internally in the Chaplin.View

On a latest change a Chaplin.Layout had been attached:

      options.title ?= @title
      options.routeLinks = false
      @layout = new Layout options

Inside the controller:

    new: (params) -&gt;
      console.log 'ListController::new'
    approved: (params) -&gt;
      console.log 'ListController::approved'

This ended up logging twice when clicked on any link on the navigation


Googling “Chaplin internal links” -> at first sight a big fat bold “routeLinks”

Set to false to deactivate internal routing. If false, chaplin won’t route links at all.

Changed Layout instantiation to:

      options.title ?= @title
      options.routeLinks = false
      @layout = new Layout options

I haven’t decided yet to use Chaplin’s internal routing or not, but at least nothing is called twice 🙂

Chaplin.mediator.suscribe handlers are called twice in a controller

  Chaplin.mediator.subscribe 'logout', @onLogout
  Chaplin.mediator.subscribe 'login', @onLogin


  onLogin: ->

Each time I logged in and out I got a saw the console calling the log several times.

Screen Shot 2014-03-13 at 13.07.49

Something was not disposed or unsuscribed correctly.

Test 1:
Unsuscribe on dispose of the controller:

      Chaplin.mediator.unsubscribe 'logout'
      Chaplin.mediator.unsubscribe 'login'

Which works… but I had other controllers running with similar event suscription but those where working correctly. Difference? Adding the context on the suscription call:

Looking like this

  Chaplin.mediator.subscribe 'logout', @onLogout, @
  Chaplin.mediator.subscribe 'login', @onLogin, @

Might be obvious for most… wasn’t for me ^^