I've been playing with a nice way of defining models in a sort of declarative way in CoffeeScript, this is what I have so far:

class Post extends Model @field 'title', default: 'New post!' @field 'body' # the default is supplied as a closure which is evaluated at object creation time @field 'created_at', default: -> new Date() class User extends Model @field 'username' @field 'twitter' @has_many Post, as: 'posts'

Which would be used like this:

# Create a new user user = new User(username: 'tom', twitter: 'almostobsolete') # Bind to the change event for the title property user.bind 'change:username', (value) => alert "Username updated to #{value}" # Print out details of posts when they are created by the user user.bind 'posts:add', (post) => alert "Post titled #{post.get('title')} was written at #{post.get('created_at')}" # Change the user's username (which will trigger the event bound above) user.set('username', 'thomas') # Create a blog post associated with the new user (which will trigger the other event) post = user.posts.create title: 'Declarative Models' body: 'One of the useful additions....'

I'm currently using something like this for an MVC architected iPhone App written in CoffeeScript. I wanted a way of defining fields and relationships in a simple, expressive and DRY way.

It works using class methods on the Model base class (as described at the end of Class section in the CoffeeScript docs). This works because the body of a class in CoffeeScript is executable code and within the body @ refers to the class object itself (elsewhere it refers to the current value of 'this').

For example, a partial implementation of the Model class (just defining @field and not including events, see here for a more complete implementation) would be:

class Model # This is the CoffeeScript syntax to declare a class method @field: (name, options) -> @fields ?= {} @fields[name] = options || {} constructor: (attributes) => @attributes = {} # Copy in attributes passed in or defaults from the fields as appropriate for name, options of @constructor.fields # Defaults can be specified as values or as functions @attributes[name] = attributes?[name] || if (isFunction(options.default)) then options.default() else options.default # Getters and setters get: (name) => @attributes[name] set: (name, value) => field = @constructor.fields[name] throw Error('Trying to set an non-existent property!') if not field # Ignore a re-setting of the same value return if value == @get(name) @attributes[name] = value

A more complete implementation (with support for everything in the example I showed at the top) is in this Gist.

The "field" class method stores information about the field in the class property named 'fields'. The constructor then uses this information to construct model instances.

This is just what I've implemented so far, I'll probably do JSON serialisation and de-serialisation next. I've taken the get/set method style from Backbone (which although I'm not using it here is great, I may write a declarative layer on top of it at some point). A much nicer syntax that would remove the need for the get and set methods could be got by using defineProperty but that would limit compatibility (it wouldn't work on Opera or IE 8, it would be fine for Node.JS though).

Overall I really like this use of CoffeeScript classes. I think it would make a great base for an ORM. I'll probably post something more complete to GitHub soon. I'm sure there are loads of other uses for this style too!