Ajax Bestiary: A Javascript Field Guide
 
Ajax Bestiary: A Javascript Field Guide
 
 

Batman.js: Micro-framework with macro features

Posted by Dave Mahon

Nick Small has released batman.js, a framework for building rich web applications with CoffeeScript or JavaScript.

Supplying an MVC architecture, data-bound HTML views and support for NodeJS, the framework is designed for single-page apps. The APIs are drawn from Rails, so RoR developers in particular will get started quickly. (See the batman-classifieds project).

It plays nicely with most other libraries, so you can (and should) still use a common DOM manipulation library when you need to do that.

The real beauty of batman.js is its custom accessors and observers.

Here’s an accessor for a box, written in CoffeeScript:

class Box extends Batman.Object
  constructor: (@length, @width, @height) ->
  @accessor 'volume',
    get: (key) -> @get('length') * @get('width') * @get('height')
 
box = new Box(16,16,12)
box.get 'volume'
# returns 3072

We just created a custom read-only property, which we can then monitor:

box.observe 'volume', (newVal, oldVal) ->
  console.log "volume changed from #{oldVal} to #{newVal}!"
box.set 'height', 6
# console output: "volume changed from 3072 to 1536!"

The next big selling point is that your designers can produce HTML and CSS to their hearts content and you, the developer, only need to embed a special data binding attribute.

To turn it into an auto-updating list, simply define the data-foreach attribute:

<div data-foreach-product="store.products" data-bind="product.name"></div>


Comments are closed.