Skip to content

Instantly share code, notes, and snippets.

@kumavis
Forked from Fordi/examples.js
Last active December 15, 2015 14:19
Show Gist options
  • Save kumavis/5274131 to your computer and use it in GitHub Desktop.
Save kumavis/5274131 to your computer and use it in GitHub Desktop.
ember-stalkable examples
###
"Stalkable" objects can be watched for any and all changes
@author Bryan Elliott <[email protected]>
###
###
Detect whether an Object is using this Mixin
###
Ember.Mixin::detectInstance = (object) ->
#jslint forin:true
name = undefined
mixins = object[Ember.META_KEY].mixins
for name of mixins
return true if @detect(mixins[name])
false
###
Changes to Ember.CoreObject that enable property stalking via the pseudoproperties: "@properties", "@ownProperties", and "@prototypeProperties"
###
Ember.Stalkable = Ember.Mixin.create(
setUnknownProperty: (key, value) ->
ret = @_super.apply(this, arguments)
@observeNewProperty key
ret
observeNewProperty: (key, isProto) ->
inst = this
isInit = inst.constructor::hasOwnProperty(key)
clean = undefined
handler = ->
clean = not inst.modifiedProperties
if clean
inst.modifiedProperties = [key]
else
inst.modifiedProperties.push key
if not isProto and not isInit
inst.notifyPropertyChange "@ownProperties"
else if isInit and not isProto
inst.notifyPropertyChange "@initProperties"
else
inst.notifyPropertyChange "@prototypeProperties"
inst.notifyPropertyChange "@properties"
delete inst.modifiedProperties if clean
@addObserver key, this, handler
handler() unless isProto
setProperties: ->
ret = undefined
@modifiedProperties = []
ret = @_super.apply(this, arguments)
delete @modifiedProperties
ret
)
###
Small modification to CoreObject.create for Stalkables
###
Ember.CoreObject.reopenClass create: ->
#jslint forin:true
i = undefined
name = undefined
args = undefined
caught = {}
ret = @_super.apply(this, arguments)
if Ember.Stalkable.detectInstance(ret)
args = [].slice.apply(arguments)
i = 0
while i < args.length
for name of args[i]
unless args[i][name] instanceof Function
ret.observeNewProperty name
caught[name] = true
i += 1
for name of ret
ret.observeNewProperty name, true unless caught[name]
ret
Ember.StalkableObject = Ember.Object.extend(Ember.Stalkable)
###
The idea here is that all changes to your new object can be observed
###
Ember.View.extend
model: Ember.StalkableObject.create()
saveModel: Ember.observer(->
###
do some fancy stuff to save changes to your model
###
, "model.@properties")
Ember.View.extend
model: Ember.StalkableObject.create(
host: null
port: null
)
saveModel: Ember.observer(->
###
this will only be called when changes to the 'host' and 'port' are changed
###
, "model.@initProperties")
Ember.View.extend
model: App.MyStalkableModel.create()
saveModel: Ember.observer(->
###
this will only be called when changes to the prototyped properties of App.MyStalkableModel are changed
###
, "model.@prototypeProperties")
Ember.View.extend
model: Ember.StalkableObject.create(nonce: null)
saveModel: Ember.observer(->
###
this will be called on changes any properties that are undefined at instantiation (i.e., excluding all the
stuff on Object, as well as "nonce", which we expect to change frequently.)
###
, "model.@ownProperties")
@kumavis
Copy link
Author

kumavis commented Mar 29, 2013

for the record, this doesnt seem to work in 1.0.0-rc.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment