Skip to content

Instantly share code, notes, and snippets.

@jareiko
Created March 9, 2013 00:19
Show Gist options
  • Save jareiko/5121636 to your computer and use it in GitHub Desktop.
Save jareiko/5121636 to your computer and use it in GitHub Desktop.
createAttributeMonitor = ->
monitored = Object.create null
(parentModel, name) ->
bubbleEvent = (event, args...) ->
return unless event.startsWith 'change:'
parentModel.trigger "change:#{name}.#{event[7..]}", args...
value = parentModel.get name
oldValue = monitored[name]
if oldValue?
return if value is oldValue
oldValue.off 'all', bubbleEvent
if value instanceof Backbone.Model or value instanceof Backbone.Collection
monitored[name] = value
value.on 'all', bubbleEvent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment