-
-
Save jbenet/4382549 to your computer and use it in GitHub Desktop.
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
class Model | |
constructor: -> | |
@data = {} | |
set: (key, val) -> | |
@data[key] = val | |
get: (key) -> | |
@data[key] | |
@property: (property, options={setter:true}) -> | |
(value) -> | |
if options.setter? and value? | |
@set property, value | |
@get(property) ? options.default | |
class Foo extends Model | |
foo: @property 'foo' | |
bar: @property('bar', setter:false) | |
baz: @property('baz', default:'Baz') | |
f = new Foo() | |
f.foo('fa') | |
console.log f.foo() | |
console.log f.data | |
f.data.foo = 'fb' | |
console.log f.foo() | |
console.log f.data | |
f.bar('ba') | |
console.log f.bar() | |
console.log f.data | |
f.data.bar = 'bb' | |
console.log f.bar() | |
console.log f.data | |
console.log f.baz() | |
console.log f.data | |
f.baz('ca') | |
console.log f.baz() | |
console.log f.data | |
f.data.baz = 'cb' | |
console.log f.baz() | |
console.log f.data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you're probably right that
{setter: undefined}
should not be considered a request for getter-only. simplest of all is justoptions.setter != false
, it's fine if we ignore0
and''