d0167fab by Michael Richards

Add initial Rivets.adapters (Default adapter for ES5 POJSOs). [#178]

1 parent 11cfcf17
...@@ -682,6 +682,49 @@ Rivets.components = {} ...@@ -682,6 +682,49 @@ Rivets.components = {}
682 # instance. 682 # instance.
683 Rivets.formatters = {} 683 Rivets.formatters = {}
684 684
685 # Rivets.adapters
686 # -----------------
687
688 # Default adapters (`.` for POJSO in ES5 natives), publicly accessible on
689 # `module.adapters`. Can be overridden globally or local to a `Rivets.View`
690 # instance.
691 Rivets.adapters =
692 '.':
693 id: '_rv'
694 counter: 0
695 weakmap: {}
696
697 subscribe: (obj, keypath, callback) ->
698 unless obj[@id]?
699 obj[@id] = @counter++
700 @weakmap[obj[@id]] = {}
701
702 map = @weakmap[obj[@id]]
703
704 unless map[keypath]?
705 map[keypath] = []
706 value = obj[keypath]
707
708 Object.defineProperty obj, keypath,
709 get: -> value
710 set: (newValue) ->
711 if newValue isnt value
712 value = newValue
713 callback() for callback in map[keypath]
714
715 unless callback in map[keypath]
716 map[keypath].push callback
717
718 unsubscribe: (obj, keypath, callback) ->
719 callbacks = @weakmap[obj[@id]][keypath]
720 callbacks.splice callbacks.indexOf(callback), 1
721
722 read: (obj, keypath) ->
723 obj[keypath]
724
725 publish: (obj, keypath, value) ->
726 obj[keypath] = value
727
685 # Rivets.config 728 # Rivets.config
686 # ------------- 729 # -------------
687 730
...@@ -709,6 +752,9 @@ Rivets.factory = (exports) -> ...@@ -709,6 +752,9 @@ Rivets.factory = (exports) ->
709 # Exposes the formatters object to be extended. 752 # Exposes the formatters object to be extended.
710 exports.formatters = Rivets.formatters 753 exports.formatters = Rivets.formatters
711 754
755 # Exposes the adapters object to be extended.
756 exports.adapters = Rivets.adapters
757
712 # Exposes the rivets configuration options. These can be set manually or from 758 # Exposes the rivets configuration options. These can be set manually or from
713 # rivets.configure with an object literal. 759 # rivets.configure with an object literal.
714 exports.config = Rivets.config 760 exports.config = Rivets.config
......