Archive

Monthly Archives: March 2014

Project Technologies Setup:

Screen Shot 2014-03-12 at 10.17.02

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

FAQ:

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

Error:

"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

seal

What’s this? –> Google: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/seal

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: https://github.com/chaplinjs/facebook-example

And there I found the piece of code, inside facebook_application.coffee

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

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

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

 

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

'ListController::new'
'ListController::new'

Googling “Chaplin internal links” -> http://docs.chaplinjs.org/chaplin.layout.html 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 🙂

http://thecodinglove.com/post/56797907621/when-i-learn-a-new-framework

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

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

Handler

  onLogin: ->
    console.log("AuthController::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:

   dispose:()->
      Chaplin.mediator.unsubscribe 'logout'
      Chaplin.mediator.unsubscribe 'login'
      super

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 ^^