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); |
11 } | 11 } |
12 | 12 |
13 /// Metadata used to label static or top-level methods that are called | 13 /// Metadata used to label static or top-level methods that are called |
14 /// automatically when loading the library of a custom element. | 14 /// automatically when loading the library of a custom element. |
15 const initMethod = const InitMethodAnnotation(); | 15 const initMethod = const InitMethodAnnotation(); |
16 | 16 |
17 /// Implementation behind [initMethod]. Only exposed for internal implementation | 17 /// Implementation behind [initMethod]. Only exposed for internal implementation |
18 /// details | 18 /// details |
19 class InitMethodAnnotation { | 19 class InitMethodAnnotation { |
20 const InitMethodAnnotation(); | 20 const InitMethodAnnotation(); |
21 } | 21 } |
22 | 22 |
23 /// This method is deprecated. It used to be where polymer initialization | 23 /// Initializes a polymer application as follows: |
24 /// happens, now this is done automatically from bootstrap.dart. | 24 /// * if running in development mode, set up a dirty-checking zone that polls |
25 @deprecated | 25 /// for observable changes |
| 26 /// * initialize template binding and polymer-element |
| 27 /// * for each library included transitively from HTML and HTML imports, |
| 28 /// register custom elements declared there (labeled with [CustomTag]) and |
| 29 /// invoke the initialization method on it (top-level functions annotated with |
| 30 /// [initMethod]). |
26 Zone initPolymer() { | 31 Zone initPolymer() { |
27 window.console.error(_ERROR); | 32 if (loader.deployMode) { |
28 return Zone.current; | 33 startPolymer(loader.initializers, loader.deployMode); |
| 34 return Zone.current; |
| 35 } |
| 36 return dirtyCheckZone()..run( |
| 37 () => startPolymer(loader.initializers, loader.deployMode)); |
29 } | 38 } |
30 | 39 |
31 const _ERROR = ''' | |
32 initPolymer is now deprecated. To initialize a polymer app: | |
33 * add to your page: <link rel="import" href="packages/polymer/polymer.html"> | |
34 * replace "application/dart" mime-types with "application/dart;component=1" | |
35 * if you use "init.dart", remove it | |
36 * if you have a main, change it into a method annotated with @initMethod | |
37 '''; | |
38 | |
39 /// True if we're in deployment mode. | 40 /// True if we're in deployment mode. |
40 bool _deployMode = false; | 41 bool _deployMode = false; |
41 | 42 |
| 43 bool _startPolymerCalled = false; |
| 44 |
42 /// Starts polymer by running all [initializers] and hooking the polymer.js | 45 /// Starts polymer by running all [initializers] and hooking the polymer.js |
43 /// code. **Note**: this function is not meant to be invoked directly by | 46 /// code. **Note**: this function is not meant to be invoked directly by |
44 /// application developers. It is invoked by a bootstrap entry point that is | 47 /// application developers. It is invoked either by [initPolymer] or, if you are |
45 /// automatically generated. During development, this entry point is generated | 48 /// using the experimental bootstrap API, this would be invoked by an entry |
46 /// dynamically in `boot.js`. Similarly, pub-build generates this entry point | 49 /// point that is automatically generated somehow. In particular, during |
47 /// for deployment. | 50 /// development, the entry point would be generated dynamically in `boot.js`. |
| 51 /// Similarly, pub-build would generate the entry point for deployment. |
48 void startPolymer(List<Function> initializers, [bool deployMode = true]) { | 52 void startPolymer(List<Function> initializers, [bool deployMode = true]) { |
| 53 if (_startPolymerCalled) throw 'Initialization was already done.'; |
| 54 _startPolymerCalled = true; |
49 _hookJsPolymer(); | 55 _hookJsPolymer(); |
50 _deployMode = deployMode; | 56 _deployMode = deployMode; |
51 | 57 |
52 for (var initializer in initializers) { | 58 for (var initializer in initializers) { |
53 initializer(); | 59 initializer(); |
54 } | 60 } |
55 } | 61 } |
56 | 62 |
| 63 /// Configures [initPolymer] making it optimized for deployment to the internet. |
| 64 /// With this setup the initializer list is supplied instead of searched for |
| 65 /// at runtime. Additionally, after this method is called [initPolymer] omits |
| 66 /// the [Zone] that automatically invokes [Observable.dirtyCheck]. |
| 67 void configureForDeployment(List<Function> initializers) { |
| 68 loader.initializers = initializers; |
| 69 loader.deployMode = true; |
| 70 } |
| 71 |
57 /// To ensure Dart can interoperate with polymer-element registered by | 72 /// To ensure Dart can interoperate with polymer-element registered by |
58 /// polymer.js, we need to be able to execute Dart code if we are registering | 73 /// polymer.js, we need to be able to execute Dart code if we are registering |
59 /// a Dart class for that element. We trigger Dart logic by patching | 74 /// a Dart class for that element. We trigger Dart logic by patching |
60 /// polymer-element's register function and: | 75 /// polymer-element's register function and: |
61 /// | 76 /// |
62 /// * if it has a Dart class, run PolymerDeclaration's register. | 77 /// * if it has a Dart class, run PolymerDeclaration's register. |
63 /// * otherwise it is a JS prototype, run polymer-element's normal register. | 78 /// * otherwise it is a JS prototype, run polymer-element's normal register. |
64 void _hookJsPolymer() { | 79 void _hookJsPolymer() { |
65 var polymerJs = js.context['Polymer']; | 80 var polymerJs = js.context['Polymer']; |
66 if (polymerJs == null) { | 81 if (polymerJs == null) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 final extendsDecl = _getDeclaration(extendee); | 113 final extendsDecl = _getDeclaration(extendee); |
99 return zone.run(() => | 114 return zone.run(() => |
100 new PolymerDeclaration(jsElem, name, type, extendsDecl).register()); | 115 new PolymerDeclaration(jsElem, name, type, extendsDecl).register()); |
101 } | 116 } |
102 // It's a JavaScript polymer element, fall back to the original register. | 117 // It's a JavaScript polymer element, fall back to the original register. |
103 return originalRegister.apply([name, extendee], thisArg: jsElem); | 118 return originalRegister.apply([name, extendee], thisArg: jsElem); |
104 } | 119 } |
105 | 120 |
106 proto['register'] = new JsFunction.withThis(registerDart); | 121 proto['register'] = new JsFunction.withThis(registerDart); |
107 } | 122 } |
OLD | NEW |