Merge pull request #61 from mikeric/utilize-adapter-read-when-subscribing
Utilize adapter.read when subscribing
Showing
3 changed files
with
18 additions
and
23 deletions
... | @@ -47,11 +47,10 @@ Rivets.js is model interface-agnostic, meaning it can work with any event-driven | ... | @@ -47,11 +47,10 @@ Rivets.js is model interface-agnostic, meaning it can work with any event-driven |
47 | rivets.configure({ | 47 | rivets.configure({ |
48 | adapter: { | 48 | adapter: { |
49 | subscribe: function(obj, keypath, callback) { | 49 | subscribe: function(obj, keypath, callback) { |
50 | callback.wrapped = function(m, v) { callback(v) }; | 50 | obj.on('change:' + keypath, callback); |
51 | obj.on('change:' + keypath, callback.wrapped); | ||
52 | }, | 51 | }, |
53 | unsubscribe: function(obj, keypath, callback) { | 52 | unsubscribe: function(obj, keypath, callback) { |
54 | obj.off('change:' + keypath, callback.wrapped); | 53 | obj.off('change:' + keypath, callback); |
55 | }, | 54 | }, |
56 | read: function(obj, keypath) { | 55 | read: function(obj, keypath) { |
57 | return obj.get(keypath); | 56 | return obj.get(keypath); | ... | ... |
... | @@ -26,7 +26,7 @@ describe('Rivets.Binding', function() { | ... | @@ -26,7 +26,7 @@ describe('Rivets.Binding', function() { |
26 | it('subscribes to the model for changes via the adapter', function() { | 26 | it('subscribes to the model for changes via the adapter', function() { |
27 | spyOn(rivets.config.adapter, 'subscribe'); | 27 | spyOn(rivets.config.adapter, 'subscribe'); |
28 | binding.bind(); | 28 | binding.bind(); |
29 | expect(rivets.config.adapter.subscribe).toHaveBeenCalledWith(model, 'name', binding.set); | 29 | expect(rivets.config.adapter.subscribe).toHaveBeenCalledWith(model, 'name', binding.sync); |
30 | }); | 30 | }); |
31 | 31 | ||
32 | describe('with preloadData set to true', function() { | 32 | describe('with preloadData set to true', function() { |
... | @@ -64,8 +64,8 @@ describe('Rivets.Binding', function() { | ... | @@ -64,8 +64,8 @@ describe('Rivets.Binding', function() { |
64 | it('sets up observers on the dependant attributes', function() { | 64 | it('sets up observers on the dependant attributes', function() { |
65 | spyOn(rivets.config.adapter, 'subscribe'); | 65 | spyOn(rivets.config.adapter, 'subscribe'); |
66 | binding.bind(); | 66 | binding.bind(); |
67 | expect(rivets.config.adapter.subscribe).toHaveBeenCalledWith(model, 'fname', binding.dependencyCallbacks['fname']); | 67 | expect(rivets.config.adapter.subscribe).toHaveBeenCalledWith(model, 'fname', binding.sync); |
68 | expect(rivets.config.adapter.subscribe).toHaveBeenCalledWith(model, 'lname', binding.dependencyCallbacks['lname']); | 68 | expect(rivets.config.adapter.subscribe).toHaveBeenCalledWith(model, 'lname', binding.sync); |
69 | }); | 69 | }); |
70 | }); | 70 | }); |
71 | }); | 71 | }); | ... | ... |
... | @@ -56,29 +56,26 @@ class Rivets.Binding | ... | @@ -56,29 +56,26 @@ class Rivets.Binding |
56 | else | 56 | else |
57 | @routine @el, value | 57 | @routine @el, value |
58 | 58 | ||
59 | # Syncs up the view binding with the model. | ||
60 | sync: => | ||
61 | @set if @options.bypass | ||
62 | @model[@keypath] | ||
63 | else | ||
64 | Rivets.config.adapter.read @model, @keypath | ||
65 | |||
59 | # Subscribes to the model for changes at the specified keypath. Bi-directional | 66 | # Subscribes to the model for changes at the specified keypath. Bi-directional |
60 | # routines will also listen for changes on the element to propagate them back | 67 | # routines will also listen for changes on the element to propagate them back |
61 | # to the model. | 68 | # to the model. |
62 | bind: => | 69 | bind: => |
63 | if @options.bypass | 70 | if @options.bypass |
64 | @set @model[@keypath] | 71 | @sync() |
65 | else | 72 | else |
66 | Rivets.config.adapter.subscribe @model, @keypath, @set | 73 | Rivets.config.adapter.subscribe @model, @keypath, @sync |
67 | 74 | @sync() if Rivets.config.preloadData | |
68 | if Rivets.config.preloadData | ||
69 | @set Rivets.config.adapter.read @model, @keypath | ||
70 | 75 | ||
71 | if @options.dependencies?.length | 76 | if @options.dependencies?.length |
72 | @dependencyCallbacks = {} | ||
73 | |||
74 | for keypath in @options.dependencies | 77 | for keypath in @options.dependencies |
75 | callback = @dependencyCallbacks[keypath] = (value) => | 78 | Rivets.config.adapter.subscribe @model, keypath, @sync |
76 | @set if @options.bypass | ||
77 | @model[@keypath] | ||
78 | else | ||
79 | Rivets.config.adapter.read @model, @keypath | ||
80 | |||
81 | Rivets.config.adapter.subscribe @model, keypath, callback | ||
82 | 79 | ||
83 | if @type in @bidirectionals | 80 | if @type in @bidirectionals |
84 | bindEvent @el, 'change', @publish | 81 | bindEvent @el, 'change', @publish |
... | @@ -91,12 +88,11 @@ class Rivets.Binding | ... | @@ -91,12 +88,11 @@ class Rivets.Binding |
91 | # Unsubscribes from the model and the element. | 88 | # Unsubscribes from the model and the element. |
92 | unbind: => | 89 | unbind: => |
93 | unless @options.bypass | 90 | unless @options.bypass |
94 | Rivets.config.adapter.unsubscribe @model, @keypath, @set | 91 | Rivets.config.adapter.unsubscribe @model, @keypath, @sync |
95 | 92 | ||
96 | if @options.dependencies?.length | 93 | if @options.dependencies?.length |
97 | for keypath in @options.dependencies | 94 | for keypath in @options.dependencies |
98 | callback = @dependencyCallbacks[keypath] | 95 | Rivets.config.adapter.unsubscribe @model, keypath, @sync |
99 | Rivets.config.adapter.unsubscribe @model, keypath, callback | ||
100 | 96 | ||
101 | if @type in @bidirectionals | 97 | if @type in @bidirectionals |
102 | @el.removeEventListener 'change', @publish | 98 | @el.removeEventListener 'change', @publish | ... | ... |
-
Please register or sign in to post a comment