f093f839 by Michael Richards

Move value parsing logic into a Rivets.View class (used to parse and store data …

…for the binded view).
1 parent cd0fca36
...@@ -49,6 +49,64 @@ ...@@ -49,6 +49,64 @@
49 49
50 })(); 50 })();
51 51
52 Rivets.View = (function() {
53
54 View.name = 'View';
55
56 function View(el, contexts) {
57 this.el = el;
58 this.contexts = contexts;
59 this.bind = __bind(this.bind, this);
60
61 this.build = __bind(this.build, this);
62
63 this.build();
64 }
65
66 View.prototype.build = function() {
67 var attribute, context, keypath, node, path, type, _i, _len, _ref, _results;
68 this.bindings = [];
69 _ref = this.el.getElementsByTagName('*');
70 _results = [];
71 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
72 node = _ref[_i];
73 _results.push((function() {
74 var _j, _len1, _ref1, _results1;
75 _ref1 = node.attributes;
76 _results1 = [];
77 for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
78 attribute = _ref1[_j];
79 if (/^data-/.test(attribute.name)) {
80 type = attribute.name.replace('data-', '');
81 path = attribute.value.split('.');
82 context = this.contexts[path.shift()];
83 keypath = path.join('.');
84 _results1.push(this.bindings.push(new Rivets.Binding(node, type, context, keypath)));
85 } else {
86 _results1.push(void 0);
87 }
88 }
89 return _results1;
90 }).call(this));
91 }
92 return _results;
93 };
94
95 View.prototype.bind = function() {
96 var binding, _i, _len, _ref, _results;
97 _ref = this.bindings;
98 _results = [];
99 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
100 binding = _ref[_i];
101 _results.push(binding.bind());
102 }
103 return _results;
104 };
105
106 return View;
107
108 })();
109
52 getInputValue = function(el) { 110 getInputValue = function(el) {
53 switch (el.type) { 111 switch (el.type) {
54 case 'text': 112 case 'text':
...@@ -130,31 +188,13 @@ ...@@ -130,31 +188,13 @@
130 return Rivets.bindings[routine] = routineFunction; 188 return Rivets.bindings[routine] = routineFunction;
131 }, 189 },
132 bind: function(el, contexts) { 190 bind: function(el, contexts) {
133 var attribute, binding, bindings, context, keypath, node, path, type, _i, _j, _k, _len, _len1, _len2, _ref, _ref1; 191 var view;
134 if (contexts == null) { 192 if (contexts == null) {
135 contexts = {}; 193 contexts = {};
136 } 194 }
137 bindings = []; 195 view = new Rivets.View(el, contexts);
138 _ref = el.getElementsByTagName('*'); 196 view.bind();
139 for (_i = 0, _len = _ref.length; _i < _len; _i++) { 197 return view;
140 node = _ref[_i];
141 _ref1 = node.attributes;
142 for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
143 attribute = _ref1[_j];
144 if (/^data-/.test(attribute.name)) {
145 type = attribute.name.replace('data-', '');
146 path = attribute.value.split('.');
147 context = path.shift();
148 keypath = path.join('.');
149 bindings.push(new Rivets.Binding(node, type, contexts[context], keypath));
150 }
151 }
152 }
153 for (_k = 0, _len2 = bindings.length; _k < _len2; _k++) {
154 binding = bindings[_k];
155 binding.bind();
156 }
157 return bindings.length;
158 } 198 }
159 }; 199 };
160 200
......
...@@ -10,7 +10,7 @@ Rivets = {} ...@@ -10,7 +10,7 @@ Rivets = {}
10 class Rivets.Binding 10 class Rivets.Binding
11 # All information about the binding is passed into the constructor; the DOM 11 # All information about the binding is passed into the constructor; the DOM
12 # element, the type of binding, the context object and the keypath at which to 12 # element, the type of binding, the context object and the keypath at which to
13 # listed to for changes. 13 # listen to for changes.
14 constructor: (@el, @type, @context, @keypath) -> 14 constructor: (@el, @type, @context, @keypath) ->
15 @routine = Rivets.bindings[@type] || attributeBinding @type 15 @routine = Rivets.bindings[@type] || attributeBinding @type
16 16
...@@ -30,6 +30,25 @@ class Rivets.Binding ...@@ -30,6 +30,25 @@ class Rivets.Binding
30 @el.addEventListener 'change', (el) => 30 @el.addEventListener 'change', (el) =>
31 Rivets.config.adapter.publish @context, @keypath, getInputValue el 31 Rivets.config.adapter.publish @context, @keypath, getInputValue el
32 32
33 class Rivets.View
34 constructor: (@el, @contexts) ->
35 @build()
36
37 build: =>
38 @bindings = []
39
40 for node in @el.getElementsByTagName '*'
41 for attribute in node.attributes
42 if /^data-/.test attribute.name
43 type = attribute.name.replace 'data-', ''
44 path = attribute.value.split '.'
45 context = @contexts[path.shift()]
46 keypath = path.join '.'
47 @bindings.push new Rivets.Binding node, type, context, keypath
48
49 bind: =>
50 binding.bind() for binding in @bindings
51
33 # Returns the current input value for the specified element. 52 # Returns the current input value for the specified element.
34 getInputValue = (el) -> 53 getInputValue = (el) ->
35 switch el.type 54 switch el.type
...@@ -83,7 +102,7 @@ Rivets.config = ...@@ -83,7 +102,7 @@ Rivets.config =
83 102
84 # The rivets module. This is the public interface that gets exported. 103 # The rivets module. This is the public interface that gets exported.
85 rivets = 104 rivets =
86 # Used to set configuration options and the adapter interface. 105 # Used to set configuration options.
87 configure: (options={}) -> 106 configure: (options={}) ->
88 for property, value of options 107 for property, value of options
89 Rivets.config[property] = value 108 Rivets.config[property] = value
...@@ -93,21 +112,12 @@ rivets = ...@@ -93,21 +112,12 @@ rivets =
93 register: (routine, routineFunction) -> 112 register: (routine, routineFunction) ->
94 Rivets.bindings[routine] = routineFunction 113 Rivets.bindings[routine] = routineFunction
95 114
96 # Binds a set of context objects to the specified DOM element. 115 # Binds a set of context objects to the specified DOM element. Returns a new
116 # Rivets.View instance.
97 bind: (el, contexts = {}) -> 117 bind: (el, contexts = {}) ->
98 bindings = [] 118 view = new Rivets.View(el, contexts)
99 119 view.bind()
100 for node in el.getElementsByTagName '*' 120 view
101 for attribute in node.attributes
102 if /^data-/.test attribute.name
103 type = attribute.name.replace 'data-', ''
104 path = attribute.value.split '.'
105 context = path.shift()
106 keypath = path.join '.'
107 bindings.push new Rivets.Binding node, type, contexts[context], keypath
108
109 binding.bind() for binding in bindings
110 bindings.length
111 121
112 # Exports rivets for both CommonJS and the browser. 122 # Exports rivets for both CommonJS and the browser.
113 if module? 123 if module?
......