Wynn Netherland changelog.com/posts

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.


Discussion

Sign in or Join to comment or subscribe

Player art
  0:00 / 0:00