Created
January 31, 2012 16:55
-
-
Save rbergman/1711563 to your computer and use it in GitHub Desktop.
A simple CoffeeScript Model base class providing declarative property support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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