Abstract out the attribute value expression parser.
Showing
1 changed file
with
27 additions
and
22 deletions
... | @@ -117,6 +117,31 @@ loopDeps = (binder, callback) -> | ... | @@ -117,6 +117,31 @@ loopDeps = (binder, callback) -> |
117 | 117 | ||
118 | callback model, keypath | 118 | callback model, keypath |
119 | 119 | ||
120 | defaultExpressionParser = (view, node, type, models, value) -> | ||
121 | pipes = (pipe.trim() for pipe in value.split '|') | ||
122 | context = (ctx.trim() for ctx in pipes.shift().split '<') | ||
123 | path = context.shift() | ||
124 | splitPath = path.split /\.|:/ | ||
125 | options = {} | ||
126 | options.formatters = pipes | ||
127 | options.bypass = path.indexOf(':') != -1 | ||
128 | options.bindContext = models | ||
129 | if splitPath[0] | ||
130 | model = models[splitPath.shift()] | ||
131 | else | ||
132 | model = models | ||
133 | splitPath.shift() | ||
134 | keypath = splitPath.join '.' | ||
135 | |||
136 | if model | ||
137 | if dependencies = context.shift() | ||
138 | options.dependencies = dependencies.split /\s+/ | ||
139 | |||
140 | binding = new Rivets.Binding node, type, model, keypath, options | ||
141 | binding.view = view | ||
142 | |||
143 | binding | ||
144 | |||
120 | # A collection of bindings built from a set of parent elements. | 145 | # A collection of bindings built from a set of parent elements. |
121 | class Rivets.View | 146 | class Rivets.View |
122 | # The DOM elements and the model objects for binding are passed into the | 147 | # The DOM elements and the model objects for binding are passed into the |
... | @@ -158,30 +183,10 @@ class Rivets.View | ... | @@ -158,30 +183,10 @@ class Rivets.View |
158 | 183 | ||
159 | for attribute in attributes or node.attributes | 184 | for attribute in attributes or node.attributes |
160 | if bindingRegExp.test attribute.name | 185 | if bindingRegExp.test attribute.name |
161 | options = {} | ||
162 | |||
163 | type = attribute.name.replace bindingRegExp, '' | 186 | type = attribute.name.replace bindingRegExp, '' |
164 | pipes = (pipe.trim() for pipe in attribute.value.split '|') | 187 | binding = defaultExpressionParser @, node, type, @models, attribute.value |
165 | context = (ctx.trim() for ctx in pipes.shift().split '<') | ||
166 | path = context.shift() | ||
167 | splitPath = path.split /\.|:/ | ||
168 | options.formatters = pipes | ||
169 | options.bypass = path.indexOf(':') != -1 | ||
170 | options.bindContext = @models | ||
171 | if splitPath[0] | ||
172 | model = @models[splitPath.shift()] | ||
173 | else | ||
174 | model = @models | ||
175 | splitPath.shift() | ||
176 | keypath = splitPath.join '.' | ||
177 | |||
178 | if model | ||
179 | if dependencies = context.shift() | ||
180 | options.dependencies = dependencies.split /\s+/ | ||
181 | |||
182 | binding = new Rivets.Binding node, type, model, keypath, options | ||
183 | binding.view = @ | ||
184 | 188 | ||
189 | if binding | ||
185 | @bindings.push binding | 190 | @bindings.push binding |
186 | 191 | ||
187 | attributes = null if attributes | 192 | attributes = null if attributes | ... | ... |
-
Please register or sign in to post a comment