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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 getInstanceModel(Element template, model) { | 84 getInstanceModel(Element template, model) { |
85 if (model is! Scope) { | 85 if (model is! Scope) { |
86 var _scope = new Scope(model: model, variables: globals); | 86 var _scope = new Scope(model: model, variables: globals); |
87 return _scope; | 87 return _scope; |
88 } | 88 } |
89 return model; | 89 return model; |
90 } | 90 } |
91 } | 91 } |
92 | 92 |
93 class _Binding extends Object with ChangeNotifierMixin { | 93 class _Binding extends Object with ChangeNotifierMixin { |
94 static const _VALUE = const Symbol('value'); | |
95 | |
96 final Scope _scope; | 94 final Scope _scope; |
97 final ExpressionObserver _expr; | 95 final ExpressionObserver _expr; |
98 final _converter; | 96 final _converter; |
99 var _value; | 97 var _value; |
100 | 98 |
101 _Binding(Expression expr, Scope scope, [this._converter]) | 99 _Binding(Expression expr, Scope scope, [this._converter]) |
102 : _expr = observe(expr, scope), | 100 : _expr = observe(expr, scope), |
103 _scope = scope { | 101 _scope = scope { |
104 _expr.onUpdate.listen(_setValue).onError((e) { | 102 _expr.onUpdate.listen(_setValue).onError((e) { |
105 _logger.warning("Error evaluating expression '$_expr': ${e.message}"); | 103 _logger.warning("Error evaluating expression '$_expr': ${e.message}"); |
106 }); | 104 }); |
107 try { | 105 try { |
108 update(_expr, _scope); | 106 update(_expr, _scope); |
109 _setValue(_expr.currentValue); | 107 _setValue(_expr.currentValue); |
110 } on EvalException catch (e) { | 108 } on EvalException catch (e) { |
111 _logger.warning("Error evaluating expression '$_expr': ${e.message}"); | 109 _logger.warning("Error evaluating expression '$_expr': ${e.message}"); |
112 } | 110 } |
113 } | 111 } |
114 | 112 |
115 _setValue(v) { | 113 _setValue(v) { |
| 114 var oldValue = _value; |
116 if (v is Comprehension) { | 115 if (v is Comprehension) { |
117 // convert the Comprehension into a list of scopes with the loop | 116 // convert the Comprehension into a list of scopes with the loop |
118 // variable added to the scope | 117 // variable added to the scope |
119 _value = v.iterable.map((i) { | 118 _value = v.iterable.map((i) { |
120 var vars = new Map(); | 119 var vars = new Map(); |
121 vars[v.identifier] = i; | 120 vars[v.identifier] = i; |
122 Scope childScope = new Scope(parent: _scope, variables: vars); | 121 Scope childScope = new Scope(parent: _scope, variables: vars); |
123 return childScope; | 122 return childScope; |
124 }).toList(growable: false); | 123 }).toList(growable: false); |
125 } else { | 124 } else { |
126 _value = (_converter == null) ? v : _converter(v); | 125 _value = (_converter == null) ? v : _converter(v); |
127 } | 126 } |
128 notifyChange(new PropertyChangeRecord(_VALUE)); | 127 notifyPropertyChange(#value, oldValue, _value); |
129 } | 128 } |
130 | 129 |
131 get value => _value; | 130 get value => _value; |
132 | 131 |
133 set value(v) { | 132 set value(v) { |
134 try { | 133 try { |
135 assign(_expr, v, _scope); | 134 assign(_expr, v, _scope); |
136 notifyChange(new PropertyChangeRecord(_VALUE)); | |
137 } on EvalException catch (e) { | 135 } on EvalException catch (e) { |
138 // silently swallow binding errors | 136 // silently swallow binding errors |
139 } | 137 } |
140 } | 138 } |
141 | |
142 getValueWorkaround(key) { | |
143 if (key == _VALUE) return value; | |
144 } | |
145 | |
146 setValueWorkaround(key, v) { | |
147 if (key == _VALUE) value = v; | |
148 } | |
149 } | 139 } |
OLD | NEW |