Created
March 9, 2013 00:18
-
-
Save jareiko/5121630 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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 |
This file contains hidden or 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
class BubbleModel extends Backbone.Model | |
initialize: -> | |
super | |
monitor = createAttributeMonitor() | |
# Bind to initial attributes. | |
monitor @, name for name of @attributes | |
# Watch for changes to attributes and rebind as necessary. | |
@on 'change', => | |
monitor @, name for name of @changed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment