| 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 part of template_binding; | 5 part of template_binding; |
| 6 | 6 |
| 7 // This code is a port of what was formerly known as Model-Driven-Views, now | 7 // This code is a port of what was formerly known as Model-Driven-Views, now |
| 8 // located at: | 8 // located at: |
| 9 // https://github.com/polymer/TemplateBinding | 9 // https://github.com/polymer/TemplateBinding |
| 10 // https://github.com/polymer/NodeBind | 10 // https://github.com/polymer/NodeBind |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 } | 250 } |
| 251 | 251 |
| 252 void _updateDependencies(_TemplateBindingMap directives, model) { | 252 void _updateDependencies(_TemplateBindingMap directives, model) { |
| 253 _closeDependencies(); | 253 _closeDependencies(); |
| 254 | 254 |
| 255 final template = _templateElement; | 255 final template = _templateElement; |
| 256 | 256 |
| 257 _hasIf = directives._if != null; | 257 _hasIf = directives._if != null; |
| 258 _hasRepeat = directives._repeat != null; | 258 _hasRepeat = directives._repeat != null; |
| 259 | 259 |
| 260 var ifValue = true; |
| 260 if (_hasIf) { | 261 if (_hasIf) { |
| 261 _ifOneTime = directives._if.onlyOneTime; | 262 _ifOneTime = directives._if.onlyOneTime; |
| 262 _ifValue = _processBinding('if', directives._if, template, model); | 263 _ifValue = _processBinding('if', directives._if, template, model); |
| 264 ifValue = _ifValue; |
| 263 | 265 |
| 264 // oneTime if & predicate is false. nothing else to do. | 266 // oneTime if & predicate is false. nothing else to do. |
| 265 if (_ifOneTime) { | 267 if (_ifOneTime && !_toBoolean(ifValue)) { |
| 266 if (!_toBoolean(_ifValue)) { | 268 _valueChanged(null); |
| 267 _updateIteratedValue(null); | 269 return; |
| 268 return; | 270 } |
| 269 } | 271 |
| 270 } else { | 272 if (!_ifOneTime) { |
| 271 (_ifValue as Bindable).open(_updateIteratedValue); | 273 ifValue = (ifValue as Bindable).open(_updateIfValue); |
| 272 } | 274 } |
| 273 } | 275 } |
| 274 | 276 |
| 275 if (_hasRepeat) { | 277 if (_hasRepeat) { |
| 276 _oneTime = directives._repeat.onlyOneTime; | 278 _oneTime = directives._repeat.onlyOneTime; |
| 277 _value = _processBinding('repeat', directives._repeat, template, model); | 279 _value = _processBinding('repeat', directives._repeat, template, model); |
| 278 } else { | 280 } else { |
| 279 _oneTime = directives._bind.onlyOneTime; | 281 _oneTime = directives._bind.onlyOneTime; |
| 280 _value = _processBinding('bind', directives._bind, template, model); | 282 _value = _processBinding('bind', directives._bind, template, model); |
| 281 } | 283 } |
| 282 | 284 |
| 283 if (!_oneTime) _value.open(_updateIteratedValue); | 285 var value = _value; |
| 286 if (!_oneTime) { |
| 287 value = _value.open(_updateIteratedValue); |
| 288 } |
| 284 | 289 |
| 285 _updateIteratedValue(null); | 290 if (!_toBoolean(ifValue)) { |
| 291 _valueChanged(null); |
| 292 return; |
| 293 } |
| 294 |
| 295 _updateValue(value); |
| 286 } | 296 } |
| 287 | 297 |
| 288 void _updateIteratedValue(_) { | 298 /// Gets the updated value of the bind/repeat. This can potentially call |
| 299 /// user code (if a bindingDelegate is set up) so we try to avoid it if we |
| 300 /// already have the value in hand (from Observer.open). |
| 301 Object _getUpdatedValue() { |
| 302 var value = _value; |
| 303 // Dart note: x.discardChanges() is x.value in Dart. |
| 304 if (!_toBoolean(_oneTime)) value = value.value; |
| 305 return value; |
| 306 } |
| 307 |
| 308 void _updateIfValue(ifValue) { |
| 309 if (!_toBoolean(ifValue)) { |
| 310 _valueChanged(null); |
| 311 return; |
| 312 } |
| 313 _updateValue(_getUpdatedValue()); |
| 314 } |
| 315 |
| 316 void _updateIteratedValue(value) { |
| 289 if (_hasIf) { | 317 if (_hasIf) { |
| 290 var ifValue = _ifValue; | 318 var ifValue = _ifValue; |
| 291 if (!_ifOneTime) ifValue = (ifValue as Bindable).value; | 319 if (!_ifOneTime) ifValue = (ifValue as Bindable).value; |
| 292 if (!_toBoolean(ifValue)) { | 320 if (!_toBoolean(ifValue)) { |
| 293 _valueChanged([]); | 321 _valueChanged([]); |
| 294 return; | 322 return; |
| 295 } | 323 } |
| 296 } | 324 } |
| 297 | 325 |
| 298 var value = _value; | 326 _updateValue(value); |
| 299 if (!_oneTime) value = (value as Bindable).value; | 327 } |
| 328 |
| 329 void _updateValue(Object value) { |
| 300 if (!_hasRepeat) value = [value]; | 330 if (!_hasRepeat) value = [value]; |
| 301 _valueChanged(value); | 331 _valueChanged(value); |
| 302 } | 332 } |
| 303 | 333 |
| 304 void _valueChanged(Object value) { | 334 void _valueChanged(Object value) { |
| 305 if (value is! List) { | 335 if (value is! List) { |
| 306 if (value is Iterable) { | 336 if (value is Iterable) { |
| 307 // Dart note: we support Iterable by calling toList. | 337 // Dart note: we support Iterable by calling toList. |
| 308 // But we need to be careful to observe the original iterator if it | 338 // But we need to be careful to observe the original iterator if it |
| 309 // supports that. | 339 // supports that. |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 _closed = true; | 547 _closed = true; |
| 518 } | 548 } |
| 519 } | 549 } |
| 520 | 550 |
| 521 // Dart note: the JavaScript version just puts an expando on the array. | 551 // Dart note: the JavaScript version just puts an expando on the array. |
| 522 class _BoundNodes { | 552 class _BoundNodes { |
| 523 final List<Node> nodes; | 553 final List<Node> nodes; |
| 524 final List<Bindable> instanceBindings; | 554 final List<Bindable> instanceBindings; |
| 525 _BoundNodes(this.nodes, this.instanceBindings); | 555 _BoundNodes(this.nodes, this.instanceBindings); |
| 526 } | 556 } |
| OLD | NEW |