OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library polymer.auto_binding; |
| 6 |
| 7 import 'dart:html'; |
| 8 import 'package:polymer/polymer.dart'; |
| 9 import 'package:template_binding/template_binding.dart'; |
| 10 |
| 11 /** |
| 12 * The `auto-binding-dart` element extends the template element. It provides a |
| 13 * quick and easy way to do data binding without the need to setup a binding |
| 14 * delegate or use the [templateBind] call. Both data and event handlers can be |
| 15 * bound using the [model]. |
| 16 * |
| 17 * The `auto-binding-dart` element acts just like a template that is bound to |
| 18 * a model. It stamps its content in the dom adjacent to itself. When the |
| 19 * content is stamped, the `template-bound` event is fired. |
| 20 * |
| 21 * **NOTE**: It is a good idea to use an id to select these elements. During |
| 22 * code transformation it is likely that other template elements will be |
| 23 * inserted at the top of the body above yours, so something like |
| 24 * querySelector('template') is likely to break when built. |
| 25 * See http://dartbug.com/20911. |
| 26 * |
| 27 * Example: |
| 28 * |
| 29 * <template id="my-template" is="auto-binding-dart"> |
| 30 * <div>Say something: <input value="{{value}}"></div> |
| 31 * <div>You said: {{value}}</div> |
| 32 * <button on-tap="{{buttonTap}}">Tap me!</button> |
| 33 * </template> |
| 34 * <script type="application/dart"> |
| 35 * import 'dart:html'; |
| 36 * import 'package:polymer/polymer.dart'; |
| 37 * |
| 38 * main() { |
| 39 * var template = document.querySelector('#my-template'); |
| 40 * template.model = new MyModel(); |
| 41 * } |
| 42 * |
| 43 * class MyModel { |
| 44 * String value = 'something'; |
| 45 * buttonTap() => console.log('tap!'); |
| 46 * } |
| 47 * </script> |
| 48 * |
| 49 */ |
| 50 // Dart note: renamed to avoid conflict with JS auto-binding. |
| 51 class AutoBindingElement extends TemplateElement with Polymer, Observable |
| 52 implements TemplateBindExtension { |
| 53 |
| 54 /// Make template_binding "extension methods" friendly. |
| 55 /// Note that [NodeBindExtension] is already implemented by [Polymer]. |
| 56 TemplateBindExtension _self; |
| 57 |
| 58 get model => _self.model; |
| 59 set model(value) { _self.model = value; } |
| 60 |
| 61 BindingDelegate get bindingDelegate => _self.bindingDelegate; |
| 62 set bindingDelegate(BindingDelegate value) { _self.bindingDelegate = value; } |
| 63 |
| 64 void clear() => _self.clear(); |
| 65 |
| 66 @override |
| 67 PolymerExpressions get syntax => bindingDelegate; |
| 68 |
| 69 AutoBindingElement.created() : super.created() { |
| 70 polymerCreated(); |
| 71 |
| 72 _self = templateBindFallback(this); |
| 73 |
| 74 bindingDelegate = makeSyntax(); |
| 75 |
| 76 // delay stamping until polymer-ready so that auto-binding is not |
| 77 // required to load last. |
| 78 Polymer.onReady.then((_) { |
| 79 attributes['bind'] = ''; |
| 80 // we don't bother with an explicit signal here, we could ust a MO |
| 81 // if necessary |
| 82 async((_) { |
| 83 // note: this will marshall *all* the elements in the parentNode |
| 84 // rather than just stamped ones. We'd need to use createInstance |
| 85 // to fix this or something else fancier. |
| 86 marshalNodeReferences(parentNode); |
| 87 // template stamping is asynchronous so stamping isn't complete |
| 88 // by polymer-ready; fire an event so users can use stamped elements |
| 89 fire('template-bound'); |
| 90 }); |
| 91 }); |
| 92 } |
| 93 |
| 94 PolymerExpressions makeSyntax() => new _AutoBindingSyntax(this); |
| 95 |
| 96 DocumentFragment createInstance([model, BindingDelegate delegate]) => |
| 97 _self.createInstance(model, delegate); |
| 98 |
| 99 @override |
| 100 dispatchMethod(obj, method, args) { |
| 101 // Dart note: make sure we dispatch to the model, not ourselves. |
| 102 if (identical(obj, this)) obj = model; |
| 103 return super.dispatchMethod(obj, method, args); |
| 104 } |
| 105 } |
| 106 |
| 107 // Dart note: this is implemented a little differently to keep it in classic |
| 108 // OOP style. Instead of monkeypatching findController, override it. |
| 109 class _AutoBindingSyntax extends PolymerExpressions { |
| 110 final AutoBindingElement _node; |
| 111 _AutoBindingSyntax(this._node) : super(); |
| 112 @override findController(_) => _node; |
| 113 } |
OLD | NEW |