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