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 polymer; | 5 part of polymer; |
6 | 6 |
7 /** | 7 /** |
8 * Use this annotation to publish a field as an attribute. For example: | 8 * Use this annotation to publish a field as an attribute. For example: |
9 * | 9 * |
10 * class MyPlaybackElement extends PolymerElement { | 10 * class MyPlaybackElement extends PolymerElement { |
(...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
513 _elementObservers = new Map<String, StreamSubscription>(); | 513 _elementObservers = new Map<String, StreamSubscription>(); |
514 } | 514 } |
515 _elementObservers[name] = sub; | 515 _elementObservers[name] = sub; |
516 } | 516 } |
517 | 517 |
518 void observeAttributeProperty(String name) { | 518 void observeAttributeProperty(String name) { |
519 _observe(name, (value, old) => reflectPropertyToAttribute(name)); | 519 _observe(name, (value, old) => reflectPropertyToAttribute(name)); |
520 } | 520 } |
521 | 521 |
522 void observeProperty(String name, Symbol method) { | 522 void observeProperty(String name, Symbol method) { |
523 final self = reflect(this); | 523 _observe(name, (value, old) => _invoke(method, [old])); |
524 _observe(name, (value, old) => self.invoke(method, [old])); | |
525 } | 524 } |
526 | 525 |
527 void observeBoth(String name, Symbol methodName) { | 526 void observeBoth(String name, Symbol methodName) { |
528 final self = reflect(this); | |
529 _observe(name, (value, old) { | 527 _observe(name, (value, old) { |
530 reflectPropertyToAttribute(name); | 528 reflectPropertyToAttribute(name); |
531 self.invoke(methodName, [old]); | 529 _invoke(methodName, [old]); |
532 }); | 530 }); |
533 } | 531 } |
534 | 532 |
535 void unbindProperty(String name) { | 533 void unbindProperty(String name) { |
536 if (_elementObservers == null) return; | 534 if (_elementObservers == null) return; |
537 var sub = _elementObservers.remove(name); | 535 var sub = _elementObservers.remove(name); |
538 if (sub != null) sub.cancel(); | 536 if (sub != null) sub.cancel(); |
539 } | 537 } |
540 | 538 |
541 void unbindAllProperties() { | 539 void unbindAllProperties() { |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
650 | 648 |
651 String findEventDelegate(Event event) => | 649 String findEventDelegate(Event event) => |
652 _declaration._eventDelegates[_eventNameFromType(event.type)]; | 650 _declaration._eventDelegates[_eventNameFromType(event.type)]; |
653 | 651 |
654 /** Call [methodName] method on [this] with [args], if the method exists. */ | 652 /** Call [methodName] method on [this] with [args], if the method exists. */ |
655 // TODO(jmesserly): I removed the [node] argument as it was unused. Reconcile. | 653 // TODO(jmesserly): I removed the [node] argument as it was unused. Reconcile. |
656 void dispatchMethod(Symbol methodName, List args) { | 654 void dispatchMethod(Symbol methodName, List args) { |
657 bool log = _eventsLog.isLoggable(Level.FINE); | 655 bool log = _eventsLog.isLoggable(Level.FINE); |
658 if (log) _eventsLog.fine('>>> [$localName]: dispatch $methodName'); | 656 if (log) _eventsLog.fine('>>> [$localName]: dispatch $methodName'); |
659 | 657 |
660 // TODO(sigmund): consider making event listeners list all arguments | 658 _invoke(methodName, args); |
661 // explicitly. Unless VM mirrors are optimized first, this reflectClass call | 659 |
662 // will be expensive once custom elements extend directly from Element (see | 660 if (log) _eventsLog.info('<<< [$localName]: dispatch $methodName'); |
663 // dartbug.com/11108). | 661 } |
| 662 |
| 663 InstanceMirror _invoke(Symbol methodName, List args) { |
| 664 // TODO(sigmund): consider making callbacks list all arguments |
| 665 // explicitly. Unless VM mirrors are optimized first, this will be expensive |
| 666 // once custom elements extend directly from Element (see issue 11108). |
664 var self = reflect(this); | 667 var self = reflect(this); |
665 var method = self.type.methods[methodName]; | 668 var method = self.type.methods[methodName]; |
666 if (method != null) { | 669 if (method != null) { |
667 // This will either truncate the argument list or extend it with extra | 670 // This will either truncate the argument list or extend it with extra |
668 // null arguments, so it will match the signature. | 671 // null arguments, so it will match the signature. |
669 // TODO(sigmund): consider accepting optional arguments when we can tell | 672 // TODO(sigmund): consider accepting optional arguments when we can tell |
670 // them appart from named arguments (see http://dartbug.com/11334) | 673 // them appart from named arguments (see http://dartbug.com/11334) |
671 args.length = method.parameters.where((p) => !p.isOptional).length; | 674 args.length = method.parameters.where((p) => !p.isOptional).length; |
672 } | 675 } |
673 self.invoke(methodName, args); | 676 return self.invoke(methodName, args); |
674 | |
675 if (log) _eventsLog.fine('<<< [$localName]: dispatch $methodName'); | |
676 } | 677 } |
677 | 678 |
678 void instanceEventListener(Event event) { | 679 void instanceEventListener(Event event) { |
679 _listenLocal(this, event); | 680 _listenLocal(this, event); |
680 } | 681 } |
681 | 682 |
682 // TODO(sjmiles): much of the below privatized only because of the vague | 683 // TODO(sjmiles): much of the below privatized only because of the vague |
683 // notion this code is too fiddly and we need to revisit the core feature | 684 // notion this code is too fiddly and we need to revisit the core feature |
684 void _listenLocal(Polymer host, Event event) { | 685 void _listenLocal(Polymer host, Event event) { |
685 // TODO(jmesserly): do we need this check? It was using cancelBubble, see: | 686 // TODO(jmesserly): do we need this check? It was using cancelBubble, see: |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
953 /** | 954 /** |
954 * Base class for PolymerElements deriving from HtmlElement. | 955 * Base class for PolymerElements deriving from HtmlElement. |
955 * | 956 * |
956 * See [Polymer]. | 957 * See [Polymer]. |
957 */ | 958 */ |
958 class PolymerElement extends HtmlElement with Polymer, Observable { | 959 class PolymerElement extends HtmlElement with Polymer, Observable { |
959 PolymerElement.created() : super.created() { | 960 PolymerElement.created() : super.created() { |
960 polymerCreated(); | 961 polymerCreated(); |
961 } | 962 } |
962 } | 963 } |
OLD | NEW |