The Changelog

Open Source moves fast. Keep up.

Batman: Fighting crime and kicking apps

Batman.js from Shopify is an extremely slick looking framework for building single page apps in Node.js.

Batman lets you define observable events on your objects via the @event macro.

class Gadget extends Batman.Object
  constructor: -> @usesLeft = 5
  use: @event (times) ->
    return false unless (@usesLeft - times) >= 0
    @usesLeft -= times

You can then observe changes to properties in this way.

gadget.observe 'name', (newVal, oldVal) ->
  console.log "name changed from #{oldVal} to #{newVal}!"
gadget.set 'name', 'Batarang'
# console output: "name changed from undefined to Batarang!"

Custom accessors also make working with object properties a lot nicer:

class Person extends Batman.Object
  constructor: (name) -> @set 'name', name
  @accessor 'name',
    get: (key) -> [@get('firstName'), @get('lastName')].join(' ')
    set: (key, val) ->
      [first, last] = val.split(' ')
      @set 'firstName', first
      @set 'lastName', last
    unset: (key) ->
      @unset 'firstName'
      @unset 'lastName'

Be sure and check out the well done project site and source code for details.


Have comments? Send a tweet to @TheChangelog on Twitter.

Subscribe to The Changelog Weekly – our weekly email covering everything that hits our open source radar.