| 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 mdv; | 5 part of mdv; |
| 6 | 6 |
| 7 /** Extensions to [Element]s that behave as templates. */ | 7 /** Extensions to [Element]s that behave as templates. */ |
| 8 class _TemplateExtension extends _ElementExtension { | 8 class _TemplateExtension extends _ElementExtension { |
| 9 var _model; | 9 var _model; |
| 10 BindingDelegate _bindingDelegate; | 10 BindingDelegate _bindingDelegate; |
| 11 _TemplateIterator _templateIterator; | 11 _TemplateIterator _templateIterator; |
| 12 bool _scheduled = false; | 12 bool _scheduled = false; |
| 13 | 13 |
| 14 _TemplateExtension(Element node) : super(node); | 14 _TemplateExtension(Element node) : super(node); |
| 15 | 15 |
| 16 void bind(String name, model, String path) { | 16 Element get node => super.node; |
| 17 |
| 18 NodeBinding createBinding(String name, model, String path) { |
| 17 switch (name) { | 19 switch (name) { |
| 18 case 'bind': | 20 case 'bind': |
| 19 case 'repeat': | 21 case 'repeat': |
| 20 case 'if': | 22 case 'if': |
| 21 if (_templateIterator == null) { | 23 if (_templateIterator == null) { |
| 22 _templateIterator = new _TemplateIterator(node); | 24 _templateIterator = new _TemplateIterator(node); |
| 23 } | 25 } |
| 24 _templateIterator.inputs.bind(name, model, path); | 26 // TODO(jmesserly): why do we do this here and nowhere else? |
| 25 return; | 27 if (path == null) path = ''; |
| 28 return new _TemplateBinding(node, name, model, path); |
| 26 default: | 29 default: |
| 27 super.bind(name, model, path); | 30 return super.createBinding(name, model, path); |
| 28 } | 31 } |
| 29 } | 32 } |
| 30 | 33 |
| 31 void unbind(String name) { | |
| 32 switch (name) { | |
| 33 case 'bind': | |
| 34 case 'repeat': | |
| 35 case 'if': | |
| 36 if (_templateIterator != null) { | |
| 37 _templateIterator.inputs.unbind(name); | |
| 38 } | |
| 39 return; | |
| 40 default: | |
| 41 super.unbind(name); | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 void unbindAll() { | |
| 46 unbind('bind'); | |
| 47 unbind('repeat'); | |
| 48 unbind('if'); | |
| 49 super.unbindAll(); | |
| 50 } | |
| 51 | |
| 52 /** | 34 /** |
| 53 * Creates an instance of the template. | 35 * Creates an instance of the template. |
| 54 */ | 36 */ |
| 55 DocumentFragment createInstance(model, BindingDelegate delegate) { | 37 DocumentFragment createInstance(model, BindingDelegate delegate) { |
| 56 var template = node.ref; | 38 var template = node.ref; |
| 57 if (template == null) template = node; | 39 if (template == null) template = node; |
| 58 | 40 |
| 59 var instance = _createDeepCloneAndDecorateTemplates( | 41 var instance = _createDeepCloneAndDecorateTemplates( |
| 60 template.ref.content, delegate); | 42 template.ref.content, delegate); |
| 61 | 43 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 91 if (_scheduled) return; | 73 if (_scheduled) return; |
| 92 _scheduled = true; | 74 _scheduled = true; |
| 93 runAsync(_setModel); | 75 runAsync(_setModel); |
| 94 } | 76 } |
| 95 | 77 |
| 96 void _setModel() { | 78 void _setModel() { |
| 97 _scheduled = false; | 79 _scheduled = false; |
| 98 _addBindings(node, _model, _bindingDelegate); | 80 _addBindings(node, _model, _bindingDelegate); |
| 99 } | 81 } |
| 100 } | 82 } |
| 83 |
| 84 class _TemplateBinding extends NodeBinding { |
| 85 // TODO(jmesserly): MDV uses TemplateIterator as the node, see: |
| 86 // https://github.com/Polymer/mdv/issues/127 |
| 87 _TemplateBinding(node, name, model, path) |
| 88 : super(node, name, model, path) { |
| 89 _mdv(node)._templateIterator.inputs.bind(property, model, path); |
| 90 } |
| 91 |
| 92 // These are no-ops because we don't use the underlying PathObserver. |
| 93 void _observePath() {} |
| 94 void boundValueChanged(newValue) {} |
| 95 |
| 96 void close() { |
| 97 if (closed) return; |
| 98 var templateIterator = _mdv(node)._templateIterator; |
| 99 if (templateIterator != null) templateIterator.inputs.unbind(property); |
| 100 super.close(); |
| 101 } |
| 102 } |
| OLD | NEW |