| 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 /** Extensions to the [Node] API. */ | 7 /** Extensions to the [Node] API. */ |
| 8 class NodeBindExtension { | 8 class NodeBindExtension { |
| 9 final Node _node; | 9 final Node _node; |
| 10 Map<String, NodeBinding> _bindings; | 10 Map<String, Bindable> _bindings; |
| 11 | 11 |
| 12 NodeBindExtension._(this._node); | 12 NodeBindExtension._(this._node); |
| 13 | 13 |
| 14 /** | 14 /** |
| 15 * Binds the attribute [name] to the [path] of the [model]. | 15 * Binds the attribute [name] to the [path] of the [model]. |
| 16 * Path is a String of accessors such as `foo.bar.baz`. | 16 * Path is a String of accessors such as `foo.bar.baz`. |
| 17 * Returns the `NodeBinding` instance. | 17 * Returns the `Bindable` instance. |
| 18 */ | 18 */ |
| 19 NodeBinding bind(String name, model, [String path]) { | 19 bind(String name, value, {bool oneTime: false}) { |
| 20 window.console.error('Unhandled binding to Node: ' | 20 window.console.error('Unhandled binding to Node: ' |
| 21 '$this $name $model $path'); | 21 '$this $name $value $oneTime'); |
| 22 } | 22 } |
| 23 | 23 |
| 24 /** Unbinds the attribute [name]. */ | 24 /** Unbinds the attribute [name]. */ |
| 25 void unbind(String name) { | 25 void unbind(String name) { |
| 26 if (_bindings == null) return; | 26 if (_bindings == null) return; |
| 27 var binding = bindings.remove(name); | 27 var binding = bindings.remove(name); |
| 28 if (binding != null) binding.close(); | 28 if (binding != null) binding.close(); |
| 29 } | 29 } |
| 30 | 30 |
| 31 /** Unbinds all bound attributes. */ | 31 /** Unbinds all bound attributes. */ |
| 32 void unbindAll() { | 32 void unbindAll() { |
| 33 if (_bindings == null) return; | 33 if (_bindings == null) return; |
| 34 for (var binding in bindings.values.toList()) { | 34 for (var binding in bindings.values.toList()) { |
| 35 if (binding != null) binding.close(); | 35 if (binding != null) binding.close(); |
| 36 } | 36 } |
| 37 _bindings = null; | 37 _bindings = null; |
| 38 } | 38 } |
| 39 | 39 |
| 40 // TODO(jmesserly): we should return a read-only wrapper here. | 40 // TODO(jmesserly): we should return a read-only wrapper here. |
| 41 /** Gets the data bindings that are associated with this node. */ | 41 /** Gets the data bindings that are associated with this node. */ |
| 42 Map<String, NodeBinding> get bindings { | 42 Map<String, Bindable> get bindings { |
| 43 if (_bindings == null) _bindings = new LinkedHashMap<String, NodeBinding>(); | 43 if (_bindings == null) _bindings = new LinkedHashMap<String, Bindable>(); |
| 44 return _bindings; | 44 return _bindings; |
| 45 } | 45 } |
| 46 | 46 |
| 47 /** | 47 /** |
| 48 * Dispatch support so custom HtmlElement's can override these methods. | 48 * Dispatch support so custom HtmlElement's can override these methods. |
| 49 * A public method like [this.bind] should not call another public method such | 49 * A public method like [this.bind] should not call another public method such |
| 50 * as [this.unbind]. Instead it should dispatch through [_self.unbind]. | 50 * as [this.unbind]. Instead it should dispatch through [_self.unbind]. |
| 51 */ | 51 */ |
| 52 NodeBindExtension get _self => _node is NodeBindExtension ? _node : this; | 52 NodeBindExtension get _self => _node is NodeBindExtension ? _node : this; |
| 53 | 53 |
| 54 TemplateInstance _templateInstance; | 54 TemplateInstance _templateInstance; |
| 55 | 55 |
| 56 /** Gets the template instance that instantiated this node, if any. */ | 56 /** Gets the template instance that instantiated this node, if any. */ |
| 57 TemplateInstance get templateInstance => | 57 TemplateInstance get templateInstance => |
| 58 _templateInstance != null ? _templateInstance : | 58 _templateInstance != null ? _templateInstance : |
| 59 (_node.parent != null ? nodeBind(_node.parent).templateInstance : null); | 59 (_node.parent != null ? nodeBind(_node.parent).templateInstance : null); |
| 60 |
| 61 _open(Bindable bindable, callback(value)) => |
| 62 callback(bindable.open(callback)); |
| 60 } | 63 } |
| 61 | 64 |
| 62 | 65 |
| 63 /** Information about the instantiated template. */ | 66 /** Information about the instantiated template. */ |
| 64 class TemplateInstance { | 67 class TemplateInstance { |
| 65 // TODO(rafaelw): firstNode & lastNode should be read-synchronous | 68 // TODO(rafaelw): firstNode & lastNode should be read-synchronous |
| 66 // in cases where script has modified the template instance boundary. | 69 // in cases where script has modified the template instance boundary. |
| 67 | 70 |
| 68 /** The first node of this template instantiation. */ | 71 /** The first node of this template instantiation. */ |
| 69 final Node firstNode; | 72 Node get firstNode => _firstNode; |
| 70 | 73 |
| 71 /** | 74 /** |
| 72 * The last node of this template instantiation. | 75 * The last node of this template instantiation. |
| 73 * This could be identical to [firstNode] if the template only expanded to a | 76 * This could be identical to [firstNode] if the template only expanded to a |
| 74 * single node. | 77 * single node. |
| 75 */ | 78 */ |
| 76 final Node lastNode; | 79 Node get lastNode => _lastNode; |
| 77 | 80 |
| 78 /** The model used to instantiate the template. */ | 81 /** The model used to instantiate the template. */ |
| 79 final model; | 82 final model; |
| 80 | 83 |
| 81 TemplateInstance(this.firstNode, this.lastNode, this.model); | 84 Node _firstNode, _lastNode; |
| 85 |
| 86 TemplateInstance(this.model); |
| 82 } | 87 } |
| OLD | NEW |