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 /// Annotation used to automatically register polymer elements. | 7 /// Annotation used to automatically register polymer elements. |
8 class CustomTag { | 8 class CustomTag { |
9 final String tagName; | 9 final String tagName; |
10 const CustomTag(this.tagName); | 10 const CustomTag(this.tagName); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 ' the platform polyfills.'); | 96 ' the platform polyfills.'); |
97 } | 97 } |
98 | 98 |
99 // TODO(jmesserly): dart:js appears to not callback in the correct zone: | 99 // TODO(jmesserly): dart:js appears to not callback in the correct zone: |
100 // https://code.google.com/p/dart/issues/detail?id=17301 | 100 // https://code.google.com/p/dart/issues/detail?id=17301 |
101 var zone = Zone.current; | 101 var zone = Zone.current; |
102 | 102 |
103 polymerJs.callMethod('whenPolymerReady', | 103 polymerJs.callMethod('whenPolymerReady', |
104 [zone.bindCallback(() => Polymer._onReady.complete())]); | 104 [zone.bindCallback(() => Polymer._onReady.complete())]); |
105 | 105 |
106 var polyElem = document.createElement('polymer-element'); | 106 JsFunction originalRegister = _polymerElementProto['register']; |
107 var proto = new JsObject.fromBrowserObject(polyElem)['__proto__']; | |
108 if (proto is Node) proto = new JsObject.fromBrowserObject(proto); | |
109 | |
110 JsFunction originalRegister = proto['register']; | |
111 if (originalRegister == null) { | 107 if (originalRegister == null) { |
112 throw new StateError('polymer.js must expose "register" function on ' | 108 throw new StateError('polymer.js must expose "register" function on ' |
113 'polymer-element to enable polymer.dart to interoperate.'); | 109 'polymer-element to enable polymer.dart to interoperate.'); |
114 } | 110 } |
115 | 111 |
116 registerDart(jsElem, String name, String extendee) { | 112 registerDart(jsElem, String name, String extendee) { |
117 // By the time we get here, we'll know for sure if it is a Dart object | 113 // By the time we get here, we'll know for sure if it is a Dart object |
118 // or not, because polymer-element will wait for us to notify that | 114 // or not, because polymer-element will wait for us to notify that |
119 // the @CustomTag was found. | 115 // the @CustomTag was found. |
120 final type = _getRegisteredType(name); | 116 final type = _getRegisteredType(name); |
121 if (type != null) { | 117 if (type != null) { |
122 final extendsDecl = _getDeclaration(extendee); | 118 final extendsDecl = _getDeclaration(extendee); |
123 return zone.run(() => | 119 return zone.run(() => |
124 new PolymerDeclaration(jsElem, name, type, extendsDecl).register()); | 120 new PolymerDeclaration(jsElem, name, type, extendsDecl).register()); |
125 } | 121 } |
126 // It's a JavaScript polymer element, fall back to the original register. | 122 // It's a JavaScript polymer element, fall back to the original register. |
127 return originalRegister.apply([name, extendee], thisArg: jsElem); | 123 return originalRegister.apply([name, extendee], thisArg: jsElem); |
128 } | 124 } |
129 | 125 |
130 proto['register'] = new JsFunction.withThis(registerDart); | 126 _polymerElementProto['register'] = new JsFunction.withThis(registerDart); |
131 } | 127 } |
| 128 |
| 129 // Note: we cache this so we can use it later to look up 'init'. |
| 130 // See registerSync. |
| 131 JsObject _polymerElementProto = () { |
| 132 var polyElem = document.createElement('polymer-element'); |
| 133 var proto = new JsObject.fromBrowserObject(polyElem)['__proto__']; |
| 134 if (proto is Node) proto = new JsObject.fromBrowserObject(proto); |
| 135 return proto; |
| 136 }(); |
OLD | NEW |