Skip to content

Instantly share code, notes, and snippets.

@jmoyers
Created May 12, 2011 00:48
Show Gist options
  • Save jmoyers/967717 to your computer and use it in GitHub Desktop.
Save jmoyers/967717 to your computer and use it in GitHub Desktop.
if not window?
inherits = require('util').inherits
EventEmitter = require('events').EventEmitter
_ = require('underscore')
extend = _.extend
keys = Object.keys
class Model extends EventEmitter
constructor: (attribs) ->
attribs |= {}
@attribs = extend(@attribs, attribs)
@track (keys(@attribs))
track: (keys) ->
if not typeof keys == Array
keys = []
for key in keys
@__defineGetter__ key, ->
@get(key)
@__defineSetter__ key, ->
@set(key)
set: (key, val, quiet) ->
@attribs[key] = val
if not quiet
@emit 'change', {key:val}
get: (key) ->
@attribs[key]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment