Skip to content

Instantly share code, notes, and snippets.

@mutewinter
Created June 4, 2014 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mutewinter/1cf612b775595c7966be to your computer and use it in GitHub Desktop.
Save mutewinter/1cf612b775595c7966be to your computer and use it in GitHub Desktop.
ObjectProxy Mixin for Ember written in CoffeeScript.
# Extracted from packages/ember-runtime/lib/system/object_proxy.js
# See the issue below for having this as a feature of Ember.
# https://github.com/emberjs/ember.js/issues/3403
{
get,
set,
meta
addObserver,
removeObserver,
addBeforeObserver,
removeBeforeObserver,
propertyWillChange,
propertyDidChange,
computed,
defineProperty,
observer,
fmt
} = Ember
contentPropertyWillChange = (content, contentKey) ->
key = contentKey.slice(8)
return if key of this
propertyWillChange this, key
contentPropertyDidChange = (content, contentKey) ->
key = contentKey.slice(8)
return if key of this
propertyDidChange this, key
App.ObjectProxyMixin = Ember.Mixin.create
content: null
_contentDidChange: observer("content", ->
Ember.assert "Can't set ObjectProxy's content to itself",
get(this, "content") isnt this
)
isTruthy: computed.bool("content")
_debugContainerKey: null
willWatchProperty: (key) ->
contentKey = "content." + key
addBeforeObserver this, contentKey, null, contentPropertyWillChange
addObserver this, contentKey, null, contentPropertyDidChange
didUnwatchProperty: (key) ->
contentKey = "content." + key
removeBeforeObserver this, contentKey, null, contentPropertyWillChange
removeObserver this, contentKey, null, contentPropertyDidChange
unknownProperty: (key) ->
content = get(this, "content")
get content, key if content
setUnknownProperty: (key, value) ->
m = meta(this)
if m.proto is this
defineProperty this, key, null, value
return value
content = get(this, "content")
Ember.assert(
fmt("Cannot delegate set('%@', %@) to the 'content' property of object" +
"proxy %@: its 'content' is undefined.", [
key
value
this
]),
content
)
set content, key, value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment