Skip to content

Instantly share code, notes, and snippets.

@tmeasday
Created June 1, 2012 02:43
Show Gist options
  • Save tmeasday/2848249 to your computer and use it in GitHub Desktop.
Save tmeasday/2848249 to your computer and use it in GitHub Desktop.
Reactive Object using getters + setters
# 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