Created
June 1, 2012 02:43
-
-
Save tmeasday/2848249 to your computer and use it in GitHub Desktop.
Reactive Object using getters + setters
This file contains 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
# pretty straightforward, very similar to the Session object, except uses getters and setters | |
# to allow reactivity to happen 'magically' | |
class ReactiveObject | |
constructor: (attributes) -> | |
@attributes = attributes | |
@contexts = {} | |
_.each attributes, (v, key) => | |
@contexts[key] = {} | |
Object.defineProperty(this, key, | |
get: -> | |
ctx = Meteor.deps.Context.current | |
@contexts[key][ctx.id] = ctx if ctx and not (ctx.id in @contexts[key]) | |
@attributes[key] | |
set: (value) -> | |
context.invalidate() for k, context of @contexts[key] | |
@contexts[key] = {} | |
@attributes[key] = value | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment