JavaScript Icon

Batman: Fighting crime and kicking apps

logged by @logbot 2011-08-25T14:44:00Z permalink #javascript #node

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.

0:00 / 0:00