5b1195cc by Michael Richards

Make adapter part of the configuration instead of an argument for rivets.bind.

1 parent fb5ebc1a
...@@ -10,9 +10,8 @@ ...@@ -10,9 +10,8 @@
10 10
11 Binding.name = 'Binding'; 11 Binding.name = 'Binding';
12 12
13 function Binding(el, adapter, type, context, keypath) { 13 function Binding(el, type, context, keypath) {
14 this.el = el; 14 this.el = el;
15 this.adapter = adapter;
16 this.type = type; 15 this.type = type;
17 this.context = context; 16 this.context = context;
18 this.keypath = keypath; 17 this.keypath = keypath;
...@@ -27,7 +26,7 @@ ...@@ -27,7 +26,7 @@
27 if (value == null) { 26 if (value == null) {
28 value = null; 27 value = null;
29 } 28 }
30 return this.routine(this.el, value || this.adapter.read(this.context, this.keypath)); 29 return this.routine(this.el, value || Rivets.config.adapter.read(this.context, this.keypath));
31 }; 30 };
32 31
33 Binding.prototype.bind = function() { 32 Binding.prototype.bind = function() {
...@@ -36,12 +35,12 @@ ...@@ -36,12 +35,12 @@
36 if (Rivets.config.preloadData) { 35 if (Rivets.config.preloadData) {
37 this.set(); 36 this.set();
38 } 37 }
39 this.adapter.subscribe(this.context, this.keypath, function(value) { 38 Rivets.config.adapter.subscribe(this.context, this.keypath, function(value) {
40 return _this.set(value); 39 return _this.set(value);
41 }); 40 });
42 if (_ref = this.type, __indexOf.call(bidirectionals, _ref) >= 0) { 41 if (_ref = this.type, __indexOf.call(bidirectionals, _ref) >= 0) {
43 return this.el.addEventListener('change', function(el) { 42 return this.el.addEventListener('change', function(el) {
44 return _this.adapter.publish(_this.context, _this.keypath, getInputValue(el)); 43 return Rivets.config.adapter.publish(_this.context, _this.keypath, getInputValue(el));
45 }); 44 });
46 } 45 }
47 }; 46 };
...@@ -130,7 +129,7 @@ ...@@ -130,7 +129,7 @@
130 register: function(routine, routineFunction) { 129 register: function(routine, routineFunction) {
131 return Rivets.bindings[routine] = routineFunction; 130 return Rivets.bindings[routine] = routineFunction;
132 }, 131 },
133 bind: function(el, adapter, contexts) { 132 bind: function(el, contexts) {
134 var attribute, binding, bindings, context, keypath, node, path, type, _i, _j, _k, _len, _len1, _len2, _ref, _ref1; 133 var attribute, binding, bindings, context, keypath, node, path, type, _i, _j, _k, _len, _len1, _len2, _ref, _ref1;
135 if (contexts == null) { 134 if (contexts == null) {
136 contexts = {}; 135 contexts = {};
...@@ -147,7 +146,7 @@ ...@@ -147,7 +146,7 @@
147 path = attribute.value.split('.'); 146 path = attribute.value.split('.');
148 context = path.shift(); 147 context = path.shift();
149 keypath = path.join('.'); 148 keypath = path.join('.');
150 bindings.push(new Rivets.Binding(node, adapter, type, contexts[context], keypath)); 149 bindings.push(new Rivets.Binding(node, type, contexts[context], keypath));
151 } 150 }
152 } 151 }
153 } 152 }
......
...@@ -6,24 +6,24 @@ ...@@ -6,24 +6,24 @@
6 Rivets = {} 6 Rivets = {}
7 7
8 class Rivets.Binding 8 class Rivets.Binding
9 constructor: (@el, @adapter, @type, @context, @keypath) -> 9 constructor: (@el, @type, @context, @keypath) ->
10 @routine = Rivets.bindings[@type] || attributeBinding @type 10 @routine = Rivets.bindings[@type] || attributeBinding @type
11 11
12 # Sets a value for this binding. Basically just runs the routine on the 12 # Sets a value for this binding. Basically just runs the routine on the
13 # element with a suplied value. 13 # element with a suplied value.
14 set: (value = null) => 14 set: (value = null) =>
15 @routine @el, value || @adapter.read @context, @keypath 15 @routine @el, value || Rivets.config.adapter.read @context, @keypath
16 16
17 # Subscribes to the context object for changes on the specific keypath. 17 # Subscribes to the context object for changes on the specific keypath.
18 # Conditionally also does the inverse and listens to the element for changes 18 # Conditionally also does the inverse and listens to the element for changes
19 # to propogate back to the context object. 19 # to propogate back to the context object.
20 bind: => 20 bind: =>
21 @set() if Rivets.config.preloadData 21 @set() if Rivets.config.preloadData
22 @adapter.subscribe @context, @keypath, (value) => @set value 22 Rivets.config.adapter.subscribe @context, @keypath, (value) => @set value
23 23
24 if @type in bidirectionals 24 if @type in bidirectionals
25 @el.addEventListener 'change', (el) => 25 @el.addEventListener 'change', (el) =>
26 @adapter.publish @context, @keypath, getInputValue el 26 Rivets.config.adapter.publish @context, @keypath, getInputValue el
27 27
28 # Returns the current input value for the specified element. 28 # Returns the current input value for the specified element.
29 getInputValue = (el) -> 29 getInputValue = (el) ->
...@@ -86,7 +86,7 @@ rivets = ...@@ -86,7 +86,7 @@ rivets =
86 register: (routine, routineFunction) -> 86 register: (routine, routineFunction) ->
87 Rivets.bindings[routine] = routineFunction 87 Rivets.bindings[routine] = routineFunction
88 88
89 bind: (el, adapter, contexts = {}) -> 89 bind: (el, contexts = {}) ->
90 bindings = [] 90 bindings = []
91 91
92 for node in el.getElementsByTagName '*' 92 for node in el.getElementsByTagName '*'
...@@ -96,7 +96,7 @@ rivets = ...@@ -96,7 +96,7 @@ rivets =
96 path = attribute.value.split '.' 96 path = attribute.value.split '.'
97 context = path.shift() 97 context = path.shift()
98 keypath = path.join '.' 98 keypath = path.join '.'
99 bindings.push new Rivets.Binding node, adapter, type, contexts[context], keypath 99 bindings.push new Rivets.Binding node, type, contexts[context], keypath
100 100
101 binding.bind() for binding in bindings 101 binding.bind() for binding in bindings
102 bindings.length 102 bindings.length
......