Skip to content

Instantly share code, notes, and snippets.

@rbergman
Created January 31, 2012 16:55
Show Gist options
  • Save rbergman/1711563 to your computer and use it in GitHub Desktop.
Save rbergman/1711563 to your computer and use it in GitHub Desktop.
A simple CoffeeScript Model base class providing declarative property support
# The Model base class
class Model
prop = (k, props) -> (v) -> if v then props[k] = v else if v is null then delete props[k] else props[k]? or null
@properties: (defaults) -> (@::[k] = prop k, (@::_props = {}))(v) for own k, v of defaults
# Example usage by a subclass
class Foo extends Model
@properties
bar: 'bar'
# Example usage of the subclass
foo = new Foo()
foo.bar() # bar
foo.bar('baz') # baz
foo.bar() # baz
foo.bar(null) # true
foo.bar() # null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment