OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * A binding delegate used with Polymer elements that | 6 * A binding delegate used with Polymer elements that |
7 * allows for complex binding expressions, including | 7 * allows for complex binding expressions, including |
8 * property access, function invocation, | 8 * property access, function invocation, |
9 * list/map indexing, and two-way filtering. | 9 * list/map indexing, and two-way filtering. |
10 * | 10 * |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 // a repeat with no identifier and we create new scope to occlude | 136 // a repeat with no identifier and we create new scope to occlude |
137 // the outer one | 137 // the outer one |
138 if (model == existingScope.model) return existingScope; | 138 if (model == existingScope.model) return existingScope; |
139 return _scopeFactory.modelScope(model: model, variables: globals); | 139 return _scopeFactory.modelScope(model: model, variables: globals); |
140 } else { | 140 } else { |
141 return _getScopeForModel(template, model); | 141 return _getScopeForModel(template, model); |
142 } | 142 } |
143 }; | 143 }; |
144 } | 144 } |
145 | 145 |
146 // We have an ident, so it's a bind/as or repeat/in expression | |
147 assert(templateBind(template).templateInstance == null); | |
148 return (model) { | 146 return (model) { |
149 var existingScope = _scopes[template]; | 147 var existingScope = _scopes[template]; |
150 if (existingScope != null) { | 148 if (existingScope != null) { |
151 // This only happens when a model has been assigned programatically | 149 // This only happens when a model has been assigned programatically |
152 // and prepareBinding is called _before_ prepareInstanceModel. | 150 // and prepareBinding is called _before_ prepareInstanceModel. |
153 // The scope assigned in prepareBinding wraps the model and is the | 151 // The scope assigned in prepareBinding wraps the model and is the |
154 // scope of the expression. That should be the parent of the templates | 152 // scope of the expression. That should be the parent of the templates |
155 // scope in the case of bind/as or repeat/in bindings. | 153 // scope in the case of bind/as or repeat/in bindings. |
156 return _scopeFactory.childScope(existingScope, ident, model); | 154 return _scopeFactory.childScope(existingScope, ident, model); |
157 } else { | 155 } else { |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 } else { | 248 } else { |
251 return _getContainingScope(node.parentNode, model); | 249 return _getContainingScope(node.parentNode, model); |
252 } | 250 } |
253 } | 251 } |
254 | 252 |
255 } | 253 } |
256 | 254 |
257 typedef Object _Converter(Object); | 255 typedef Object _Converter(Object); |
258 | 256 |
259 class _Binding extends Bindable { | 257 class _Binding extends Bindable { |
260 static int __seq = 1; | |
261 | |
262 final int _seq = __seq++; | |
263 | |
264 final Scope _scope; | 258 final Scope _scope; |
265 final _Converter _converter; | 259 final _Converter _converter; |
266 final Expression _expr; | 260 final Expression _expr; |
| 261 |
267 Function _callback; | 262 Function _callback; |
268 StreamSubscription _sub; | 263 StreamSubscription _sub; |
| 264 ExpressionObserver _observer; |
269 var _value; | 265 var _value; |
270 | 266 |
271 _Binding(this._expr, this._scope, [converter]) | 267 _Binding(this._expr, this._scope, [converter]) |
272 : _converter = converter == null ? _identity : converter; | 268 : _converter = converter == null ? _identity : converter; |
273 | 269 |
274 static Object _oneTime(Expression expr, Scope scope, _Converter converter) { | 270 static Object _oneTime(Expression expr, Scope scope, _Converter converter) { |
275 try { | 271 try { |
276 var value = eval(expr, scope); | 272 var value = eval(expr, scope); |
277 return (converter == null) ? value : converter(value); | 273 return (converter == null) ? value : converter(value); |
278 } catch (e, s) { | 274 } catch (e, s) { |
(...skipping 25 matching lines...) Expand all Loading... |
304 } catch (e, s) { | 300 } catch (e, s) { |
305 new Completer().completeError( | 301 new Completer().completeError( |
306 "Error evaluating expression '$_expr': $e", s); | 302 "Error evaluating expression '$_expr': $e", s); |
307 } | 303 } |
308 } | 304 } |
309 | 305 |
310 Object open(callback(value)) { | 306 Object open(callback(value)) { |
311 if (_callback != null) throw new StateError('already open'); | 307 if (_callback != null) throw new StateError('already open'); |
312 | 308 |
313 _callback = callback; | 309 _callback = callback; |
314 final expr = observe(_expr, _scope); | 310 _observer = observe(_expr, _scope); |
315 // _expr = expr; | 311 _sub = _observer.onUpdate.listen(_check)..onError((e, s) { |
316 _sub = expr.onUpdate.listen(_check)..onError((e, s) { | |
317 new Completer().completeError( | 312 new Completer().completeError( |
318 "Error evaluating expression '$expr': $e", s); | 313 "Error evaluating expression '$_observer': $e", s); |
319 }); | 314 }); |
| 315 |
320 try { | 316 try { |
321 // this causes a call to _updateValue with the new value | 317 // this causes a call to _updateValue with the new value |
322 update(expr, _scope); | 318 update(_observer, _scope); |
323 _check(expr.currentValue, skipChanges: true); | 319 _check(_observer.currentValue, skipChanges: true); |
324 } catch (e, s) { | 320 } catch (e, s) { |
325 new Completer().completeError( | 321 new Completer().completeError( |
326 "Error evaluating expression '$expr': $e", s); | 322 "Error evaluating expression '$_observer': $e", s); |
327 } | 323 } |
328 return _value; | 324 return _value; |
329 } | 325 } |
330 | 326 |
331 void close() { | 327 void close() { |
332 if (_callback == null) return; | 328 if (_callback == null) return; |
333 | 329 |
334 _sub.cancel(); | 330 _sub.cancel(); |
335 _sub = null; | 331 _sub = null; |
336 _callback = null; | 332 _callback = null; |
| 333 |
| 334 new Closer().visit(_observer); |
| 335 _observer = null; |
337 } | 336 } |
338 } | 337 } |
339 | 338 |
340 _identity(x) => x; | 339 _identity(x) => x; |
341 | 340 |
342 /** | 341 /** |
343 * Factory function used for testing. | 342 * Factory function used for testing. |
344 */ | 343 */ |
345 class ScopeFactory { | 344 class ScopeFactory { |
346 const ScopeFactory(); | 345 const ScopeFactory(); |
347 modelScope({Object model, Map<String, Object> variables}) => | 346 modelScope({Object model, Map<String, Object> variables}) => |
348 new Scope(model: model, variables: variables); | 347 new Scope(model: model, variables: variables); |
349 | 348 |
350 childScope(Scope parent, String name, Object value) => | 349 childScope(Scope parent, String name, Object value) => |
351 parent.childScope(name, value); | 350 parent.childScope(name, value); |
352 } | 351 } |
OLD | NEW |