OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import 'dart:core'; | 5 import 'dart:core'; |
6 import 'dart:html'; | 6 import 'dart:html'; |
7 import 'dart:js'; | 7 import 'dart:js'; |
| 8 @MirrorsUsed(metaTargets: const [BindableAnnotation]) |
8 import 'dart:mirrors'; | 9 import 'dart:mirrors'; |
9 import 'package:js/js.dart'; | 10 import 'package:js/js.dart'; |
10 import 'package:js_util/js_util.dart'; | 11 import 'package:js_util/js_util.dart'; |
11 import 'package:polymer/polymer.dart'; | 12 import 'package:polymer/polymer.dart'; |
12 | 13 |
13 class Binding { | 14 const BindableAnnotation bindable = const BindableAnnotation(); |
14 final String attribute; | 15 class BindableAnnotation { |
15 final String property; | 16 const BindableAnnotation(); |
16 const Binding (attribute, [String property]) | |
17 : attribute = attribute, | |
18 property = property == null ? attribute : property; | |
19 } | 17 } |
20 | 18 |
| 19 |
21 ///This is a temporary bridge between Polymer Bindings and the wrapper entities. | 20 ///This is a temporary bridge between Polymer Bindings and the wrapper entities. |
22 class Binder<T extends HtmlElement> { | 21 class Binder<T extends HtmlElement> { |
23 final List<Binding> attributes; | 22 final Map<String, Symbol> attributes; |
24 final callback; | |
25 | 23 |
26 Binder(List<Binding> attributes) | 24 const Binder(Map<String, Symbol> attributes) |
27 : attributes = attributes, | 25 : attributes = attributes; |
28 callback = _createCallback(T, attributes); | |
29 | 26 |
30 registerCallback(T element) { | 27 registerCallback(T element) { |
31 assert(element != null); | 28 assert(element != null); |
32 setValue(element, 'bind', callback); | 29 setValue(element, 'bind', allowInteropCaptureThis(_callback)); |
33 } | 30 } |
34 | 31 |
35 static _createCallback(Type T, List<Binding> attributes){ | 32 void _callback(_this, name, value, [other]) { |
36 final target = reflectClass(T); | 33 final setter = attributes[name]; |
37 final setters = <String, Symbol>{}; | 34 if (setter == null) return; |
38 for (Binding binding in attributes){ | 35 Bindable bindable; |
39 var member = target.instanceMembers[new Symbol(binding.property + '=')]; | 36 if (identical(1, 1.0)) { // dart2js |
40 if (!member.isSetter) | 37 bindable = getValue(getValue(value, '__dartBindable'), 'o') as Bindable; |
41 throw new ArgumentError( | 38 } else { // vm |
42 '${binding.property} is not a Setter for class $T'); | 39 bindable = getValue(value, '__dartBindable'); |
43 setters[binding.attribute] = new Symbol(binding.property); | |
44 } | 40 } |
45 return allowInteropCaptureThis((_this, name, value, [other]) { | 41 var obj = reflect(_this); |
46 final setter = setters[name]; | 42 obj.setField(setter, bindable.value); |
47 if (setter == null) return; | 43 bindable.open((value) { |
48 Bindable bindable; | 44 obj.setField(setter, value); |
49 if (identical(1, 1.0)) { // dart2js | |
50 bindable = getValue(getValue(value, '__dartBindable'), 'o') as Bindable; | |
51 } else { // vm | |
52 bindable = getValue(value, '__dartBindable'); | |
53 } | |
54 var obj = reflect(_this); | |
55 obj.setField(setter, bindable.value); | |
56 bindable.open((value) { | |
57 obj.setField(setter, value); | |
58 }); | |
59 }); | 45 }); |
60 } | 46 } |
61 } | 47 } |
OLD | NEW |