Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 --- | 1 --- |
| 2 layout: tutorial | 2 layout: tutorial |
| 3 title: "Define a Custom Element" | 3 title: "Define a Custom Element" |
| 4 description: "Create a custom HTML element using Polymer." | 4 description: "Create a custom HTML element using Polymer." |
| 5 has-permalinks: true | 5 has-permalinks: true |
| 6 tutorial: | 6 tutorial: |
| 7 id: polymer-intro | 7 id: polymer-intro |
| 8 next: futures/ | 8 next: futures/ |
| 9 next-title: "Use Future-Based APIs" | 9 next-title: "Use Future-Based APIs" |
| 10 prev: shared-pkgs/ | 10 prev: shared-pkgs/ |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 <h3>Create a custom HTML element using Polymer</h3> | 42 <h3>Create a custom HTML element using Polymer</h3> |
| 43 </div> | 43 </div> |
| 44 | 44 |
| 45 A custom element is an HTML element you can define yourself, | 45 A custom element is an HTML element you can define yourself, |
| 46 encapsulating appearance and/or behavior | 46 encapsulating appearance and/or behavior |
| 47 within semantically meaningful HTML. | 47 within semantically meaningful HTML. |
| 48 | 48 |
| 49 <aside class="alert"> | 49 <aside class="alert"> |
| 50 <strong>Version Note:</strong> The code sample and the content | 50 <strong>Version Note:</strong> The code sample and the content |
| 51 of this tutorial are compatible with | 51 of this tutorial are compatible with |
| 52 <a href="https://pub.dartlang.org/packages/polymer#versions">polymer.dart 0.9</a >. | 52 <a href="https://pub.dartlang.org/packages/polymer#versions">polymer.dart 0.10.< /a>. |
| 53 </aside> | 53 </aside> |
| 54 | 54 |
| 55 <aside class="alert alert-info"> | 55 <aside class="alert alert-info"> |
| 56 Custom elements are one feature of | 56 Custom elements are one feature of |
| 57 <a href="http://www.polymer-project.org/" | 57 <a href="http://www.polymer-project.org/" |
| 58 target="_blank">Polymer</a>, | 58 target="_blank">Polymer</a>, |
| 59 a new type of library for the web based on Web Components. | 59 a new type of library for the web based on Web Components. |
| 60 <a href="http://www.dartlang.org/polymer-dart/" | 60 <a href="http://www.dartlang.org/polymer-dart/" |
| 61 target="_blank">Polymer.dart</a> | 61 target="_blank">Polymer.dart</a> |
| 62 is the Dart implementation of Polymer. | 62 is the Dart implementation of Polymer. |
| 63 (Note: Polymer supersedes Web UI.) | |
| 64 </aside> | 63 </aside> |
| 65 | 64 |
| 66 * [An example](#an-example) | 65 * [An example](#an-example) |
| 67 * [Installing Polymer.dart](#getting-polymer-dart) | 66 * [Installing Polymer.dart](#getting-polymer-dart) |
| 68 * [Including Polymer.dart in your application](#bootstrap) | 67 * [Including Polymer.dart in your application](#bootstrap) |
| 69 * [Instantiating a custom element](#instantiating) | 68 * [Instantiating a custom element](#instantiating) |
| 70 * [Defining a custom element](#define-element) | 69 * [Defining a custom element](#define-element) |
| 71 * [Providing a template for the custom element](#providing-a-template) | 70 * [Providing a template for the custom element](#providing-a-template) |
| 72 * [Providing a script for the custom element](#providing-a-script) | 71 * [Providing a script for the custom element](#providing-a-script) |
| 73 * [Overiding life-cycle methods](#life-cycle-methods) | 72 * [Overiding life-cycle methods](#life-cycle-methods) |
| 74 * [Using data binding](#data-binding) | 73 * [Using data binding](#data-binding) |
| 75 * [Setting up event handlers declaratively](#event-handlers) | 74 * [Setting up event handlers declaratively](#event-handlers) |
| 76 * [Styling a custom element](#scoped-css) | 75 * [Styling a custom element](#scoped-css) |
| 76 * [Deploying an app that uses Polymer](#deploying-an-app-that-uses-polymer) | |
| 77 * [Other resources](#other-resources) | 77 * [Other resources](#other-resources) |
| 78 * [What next?](#what-next) | 78 * [What next?](#what-next) |
| 79 | 79 |
| 80 ##An example | 80 ##An example |
| 81 | 81 |
| 82 In the example running below, | 82 In the example running below, |
| 83 the LemonChiffon area outlined in black | 83 the LemonChiffon area outlined in black |
| 84 is a custom element implemented using Polymer. | 84 is a custom element implemented using Polymer. |
| 85 | 85 |
| 86 <strong>Try it!</strong> | 86 <strong>Try it!</strong> |
| 87 Start and stop the stopwatch. | 87 Start and stop the stopwatch. |
| 88 Reset the stopwatch to 00:00 using the **Reset** button. | 88 Reset the stopwatch to 00:00 using the **Reset** button. |
| 89 | 89 |
| 90 <iframe class="running-app-frame" | 90 <iframe class="running-app-frame" |
| 91 style="height:220px;width:230px;" | 91 style="height:220px;width:230px;" |
| 92 src="examples/stopwatch/out/web/index.html"> | 92 src="examples/stopwatch/web/index.html"> |
| 93 </iframe> | 93 </iframe> |
| 94 | 94 |
| 95 To place this custom element on an HTML page, | 95 To place this custom element on an HTML page, |
| 96 import the file with the custom element definition | 96 import `packages/polymer/polymer.html` |
| 97 and the HTML file with the custom element definition, | |
| 97 and use the name of the element as an HTML tag: | 98 and use the name of the element as an HTML tag: |
| 98 | 99 |
| 99 {% prettify html %} | 100 {% prettify html %} |
| 101 <link rel="import" href="packages/polymer/polymer.html"> | |
| 100 <link rel="import" href="tute_stopwatch.html"> | 102 <link rel="import" href="tute_stopwatch.html"> |
| 101 ... | 103 ... |
| 102 <tute-stopwatch></tute-stopwatch> | 104 <tute-stopwatch></tute-stopwatch> |
| 103 {% endprettify %} | 105 {% endprettify %} |
| 104 | 106 |
| 105 The counting text, the three buttons along with their actions, | 107 The counting text, the three buttons along with their actions, |
| 106 and the style are all contained within the custom element. | 108 and the style are all contained within the custom element. |
| 107 The definition of the custom element encapsulates and | 109 The definition of the custom element encapsulates and |
| 108 hides the implementation details, | 110 hides the implementation details, |
| 109 which as the user of the element, you care nothing about. | 111 which as the user of the element, you care nothing about. |
| 110 | 112 |
| 111 When you use developer tools to inspect the element, | |
| 112 you see just the custom element's begin and end tags. | |
| 113 | |
| 114 <img class="scale-img-max" src="images/dev-tools-dom.png" | |
| 115 alt="Custom element encapsulates its contents"> | |
| 116 | |
| 117 With custom elements, you can easily create new kinds of elements | 113 With custom elements, you can easily create new kinds of elements |
| 118 that have semantically meaningful tags and that are easy to share, | 114 that have semantically meaningful tags and that are easy to share, |
| 119 reuse, and read. | 115 reuse, and read. |
| 120 | 116 |
| 121 ###Overview of the example files | 117 ###Overview of the example files |
| 122 | 118 |
| 123 Three main source files implement the Stopwatch example: | 119 Three main source files implement the Stopwatch example: |
| 124 | 120 |
| 125 <dl> | 121 <dl> |
| 126 <dt> | 122 <dt> |
| 127 index.html | 123 index.html |
| 128 </dt> | 124 </dt> |
| 129 <dd> | 125 <dd> |
| 130 The primary HTML file for the app. | 126 The primary HTML file for the app. |
| 131 Includes the Polymer bootstrap script and instantiates the custom element. | 127 It imports HTML files for Polymer and the custom element, |
| 128 and it instantiates the custom element. | |
| 132 </dd> | 129 </dd> |
| 133 <dt> | 130 <dt> |
| 134 tute_stopwatch.html | 131 tute_stopwatch.html |
| 135 </dt> | 132 </dt> |
| 136 <dd> | 133 <dd> |
| 137 The HTML code that defines the custom element. | 134 The HTML code that defines the custom element. |
| 138 </dd> | 135 </dd> |
| 139 <dt> | 136 <dt> |
| 140 tute_stopwatch.dart | 137 tute_stopwatch.dart |
| 141 </dt> | 138 </dt> |
| 142 <dd> | 139 <dd> |
| 143 The Dart class that implements the custom element. | 140 The Dart class that implements the custom element. |
| 144 </dd> | 141 </dd> |
| 145 </dl> | 142 </dl> |
| 146 | 143 |
| 147 The following diagram shows the structure of the example | 144 The following diagram shows the structure of the example |
| 148 app and its use of custom elements. | 145 app and its use of custom elements: |
| 146 | |
| 147 * <span style="background: rgb(249, 238, 172);"> | |
| 148 Import Polymer</span> | |
| 149 * <span style="background: rgb(190, 249, 221);"> | |
| 150 Import custom element definition</span> | |
| 151 * <span style="background: rgb(209, 226, 254);"> | |
| 152 Define, implement, and instantiate custom element by name</span> | |
| 153 * <span style="background: rgb(242, 209, 235);"> | |
| 154 Associate Dart class with custom element definition</span> | |
| 149 | 155 |
| 150 <img class="scale-img-max" src="images/connections.png" | 156 <img class="scale-img-max" src="images/connections.png" |
| 151 alt="The connections between Dart and HTML"> | 157 alt="The connections between Dart and HTML"> |
| 152 | 158 |
| 159 | |
| 153 ##Installing Polymer.dart {#getting-polymer-dart} | 160 ##Installing Polymer.dart {#getting-polymer-dart} |
| 154 | 161 |
| 155 To use the features provided by Polymer.dart, | 162 To use the features provided by Polymer.dart, |
| 156 you need to install the Polymer package. | 163 you need to install the Polymer package. |
| 157 If you are unfamiliar with installing packages, | 164 If you are unfamiliar with installing packages, |
| 158 refer to | 165 refer to |
| 159 <a href="/docs/tutorials/shared-pkgs/">Install Shared Packages</a>, | 166 <a href="/docs/tutorials/shared-pkgs/">Install Shared Packages</a>, |
| 160 which describes the process in detail. | 167 which describes the process in detail. |
| 161 | 168 |
| 162 In brief, to install the Polymer package: | 169 In brief, to install the Polymer package: |
| 163 | 170 |
| 164 * In the application's `pubspec.yaml` file, | 171 <ul markdown="1"> |
| 172 <li markdown="1"> | |
| 173 In the application's `pubspec.yaml` file, | |
| 165 add the package to the list of dependencies | 174 add the package to the list of dependencies |
| 166 by adding the package name, `polymer`, to the list. | 175 by adding the package name, `polymer`, to the list. |
| 176 | |
| 177 <pre> | |
| 178 name: stopwatch | |
| 179 description: A sample application | |
| 180 dependencies: | |
| 181 <b>polymer: ">=0.10.0 <0.11.0"</b> | |
| 182 </pre> | |
| 183 </li> | |
| 184 | |
| 185 <aside class="alert alert-warning"> | |
| 186 <b>Important:</b> | |
| 167 YAML is whitespace-sensitive, | 187 YAML is whitespace-sensitive, |
| 168 so take care to indent the package name as shown: | 188 so take care to indent the package name as shown. |
| 189 </aside> | |
| 169 | 190 |
| 170 <img class="scale-img-max" src="images/sample-pubspec.png" | 191 <li markdown="1"> |
| 171 alt="Sample pubspec file with polymer dependency"> | 192 Run `pub get`, |
| 172 | |
| 173 * Run `pub get`, | |
| 174 which recursively installs the polymer.dart package | 193 which recursively installs the polymer.dart package |
| 175 and all the packages that it depends on. | 194 and all the packages that it depends on. |
| 176 If you are using Dart Editor, | 195 If you are using Dart Editor, |
| 177 when you save pubspec.yaml | 196 when you save pubspec.yaml |
| 178 the editor automatically runs `pub get` for you. | 197 the editor automatically runs `pub get` for you. |
| 179 If you are using command line tools, | 198 If you are using command-line tools, |
| 180 you can run it with the command `pub get`. | 199 you can use the command `pub get`. |
| 200 </li> | |
| 201 </ul> | |
| 181 | 202 |
| 182 ##Including Polymer.dart in your application {#bootstrap} | 203 ##Including Polymer.dart in your application {#bootstrap} |
| 183 | 204 |
| 184 To use Polymer.dart features such as custom elements, | 205 To use Polymer.dart features such as custom elements, |
| 185 you need to include Polymer in both | 206 you need to include Polymer in both |
| 186 the HTML side and the Dart side of your app. | 207 the HTML side and the Dart side of your app. |
| 187 | 208 |
| 188 * In the primary HTML file for your app, | 209 <aside class="alert alert-warning"> |
| 189 export `package:polymer/init.dart` within a <script> tag | 210 <b>Important:</b> |
| 190 in the <head> section. | 211 Unlike other Dart web apps, |
| 191 This script contains the `main()` function | 212 Polymer apps have no <tty>main()</tty> function |
| 192 for the app and initializes Polymer. | 213 and do not use <tty>packages/browser/dart.js</tty>. |
| 193 | 214 </aside> |
| 194 <img class="scale-img-max" src="images/init-script.png" | |
| 195 alt="Include the Polymer init script"> | |
| 196 | 215 |
| 197 * In the primary HTML file for your app, | 216 * In the primary HTML file for your app, |
| 198 include the `packages/browser/dart.js` bootstrap script | 217 import `packages/polymer/polymer.html` |
| 199 in the <head> section. | 218 in the the <head> section: |
| 200 | 219 |
| 201 <img class="scale-img-max" src="images/bootstrap-script.png" | 220 <pre> |
| 202 alt="Include the Polymer bootstrap script"> | 221 <head> |
| 222 ... | |
| 223 <b><link rel="import" href="packages/polymer/polymer.html"></b> | |
| 224 ... | |
| 225 </head> | |
| 226 </pre> | |
| 203 | 227 |
| 204 * In your Dart code, import the Polymer library: | 228 * In your Dart code, import the Polymer library: |
| 205 | 229 |
| 206 <img class="scale-img-max" src="images/polymer-library.png" | 230 <pre> |
| 207 alt="Import the Polymer library"> | 231 import 'dart:html'; |
| 232 import 'dart:async'; | |
| 233 <b>import 'package:polymer/polymer.dart';</b> | |
| 234 ... | |
| 235 </pre> | |
| 208 | 236 |
| 209 ##Instantiating a custom element {#instantiating} | 237 ##Instantiating a custom element {#instantiating} |
| 210 | 238 |
| 211 To create an instance of a custom element, | 239 To create an instance of a custom element, |
| 212 use the name of the custom element just as you would any normal HTML tag. | 240 use the name of the custom element just as you would any normal HTML tag. |
| 213 In this example, the tag name is `tute-stopwatch`. | 241 In this example, the tag name is `tute-stopwatch`. |
| 214 | 242 |
| 215 <img class="scale-img-max" src="images/polymer-instance-create.png" | 243 <img class="scale-img-max" src="images/polymer-instance-create.png" |
| 216 alt="Instantiate a custom element with a custom tag"> | 244 alt="Instantiate a custom element with a custom tag"> |
| 217 | 245 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 249 typically have both a template and a script, | 277 typically have both a template and a script, |
| 250 but neither is required. | 278 but neither is required. |
| 251 A custom element with a script and no template is purely functional. | 279 A custom element with a script and no template is purely functional. |
| 252 A custom element with a template and no script is purely visual. | 280 A custom element with a template and no script is purely visual. |
| 253 | 281 |
| 254 {% prettify html %} | 282 {% prettify html %} |
| 255 <polymer-element name="tute-stopwatch"> | 283 <polymer-element name="tute-stopwatch"> |
| 256 <template> | 284 <template> |
| 257 ... | 285 ... |
| 258 </template> | 286 </template> |
| 259 <script type="application/dart" src="tute_stopwatch.dart"> | 287 <script type="application/dart;component=1" |
| 288 src="tute_stopwatch.dart"> | |
| 260 </script> | 289 </script> |
| 261 </polymer-element> | 290 </polymer-element> |
| 262 {% endprettify %} | 291 {% endprettify %} |
| 263 | 292 |
| 264 <dl> | 293 <dl> |
| 265 <dt> <template> </dt> | 294 <dt> <template> </dt> |
| 266 <dd> | 295 <dd> |
| 267 Describes the custom element's structure—its user interface. | 296 Describes the custom element's structure—its user interface. |
| 268 The template comprises any valid HTML code within the <template> tag. | 297 The template comprises any valid HTML code within the <template> tag. |
| 269 When the custom element is instantiated, | 298 When the custom element is instantiated, |
| 270 the instance is created from the template. | 299 the instance is created from the template. |
| 271 The template can include CSS styles within a <style> tag. | 300 The template can include CSS styles within a <style> tag. |
| 272 </dd> | 301 </dd> |
| 273 | 302 |
| 274 <dt> <script> </dt> | 303 <dt> <script> </dt> |
| 275 <dd markdown="1"> Specifies a Dart script. | 304 <dd markdown="1"> Specifies a Dart script. |
| 276 For custom elements, the Dart script is a Dart class | 305 For custom elements, the Dart script is a Dart class |
| 277 that implements the behavior of the element. | 306 that implements the behavior of the element. |
| 278 The class typically overrides some | 307 The class typically overrides some |
| 279 life-cycle methods and provides event handlers | 308 life-cycle methods and provides event handlers |
| 280 that join the UI with its programmatic behavior. | 309 that join the UI with its programmatic behavior. |
| 281 In this example, the script is in tute_stopwatch.dart. | 310 In this example, the script is in tute_stopwatch.dart. |
| 311 The script type for custom elements must be | |
| 312 "application/dart;component=1". | |
| 282 </dd> | 313 </dd> |
| 283 </dl> | 314 </dl> |
| 284 | 315 |
| 285 ##Providing a template for the custom element {#providing-a-template} | 316 ##Providing a template for the custom element {#providing-a-template} |
| 286 | 317 |
| 287 Here's the template code for the tute-stopwatch element: | 318 Here's the template code for the tute-stopwatch element: |
| 288 | 319 |
| 289 <img class="scale-img-max" src="images/template-code.png" | 320 <img class="scale-img-max" src="images/template-code.png" |
| 290 alt="The template code for the tute-stopwatch element"> | 321 alt="The template code for the tute-stopwatch element"> |
| 291 | 322 |
| 292 The tute-stopwatch template uses a <style> tag, which is optional. | 323 The tute-stopwatch template uses a <style> tag, which is optional. |
| 293 These styles are scoped; they affect only | 324 These styles are scoped; they affect only |
| 294 the appearance of the custom element and the elements it contains. | 325 the appearance of the custom element and the elements it contains. |
| 295 More about scoped CSS in [Styling a custom element](#scoped-css). | 326 More about scoped CSS in [Styling a custom element](#scoped-css). |
| 296 | 327 |
| 297 The rest of the code within the <template> tag | 328 The rest of the code within the <template> tag |
| 298 is normal HTML, with two exceptions: | 329 is normal HTML, with two exceptions: |
| 299 | 330 |
| 300 |---|---| | 331 |---|---| |
| 301 | `{``{``counter}}` | Uses a Polymer syntax to [bind Dart data](#data-binding) t o the HTML page. The double curly braces are commonly known as a "double mustach e". | | 332 | `{``{`<em><code>expression</code></em>`}}` | Uses a Polymer syntax to [bind Da rt data](#data-binding) to the HTML page. The double curly braces are commonly k nown as a "double mustache". | |
| 302 | `on-click` | Uses Polymer [declarative event mapping](#event-handlers), which allows you to set up event handlers for a UI element. `on-click` sets up an even t handler for mouse clicks. Polymer has mappings for other event types, such as `on-input` for changes to text fields. | | 333 | `on-click` | Uses Polymer [declarative event mapping](#event-handlers), which allows you to set up event handlers for a UI element. `on-click` sets up an even t handler for mouse clicks. Polymer has mappings for other event types, such as `on-input` for changes to text fields. | |
| 303 {: .table} | 334 {: .table} |
| 304 | 335 |
| 305 Let's take a look at the structure of the Dart code | 336 Let's take a look at the structure of the Dart code |
| 306 before we get into the details of data binding, event handlers, | 337 before we get into the details of data binding, event handlers, |
| 307 and scoped CSS. | 338 and scoped CSS. |
| 308 | 339 |
| 309 ##Providing a script for the custom element {#providing-a-script} | 340 ##Providing a script for the custom element {#providing-a-script} |
| 310 | 341 |
| 311 On the Dart side, a class implements the behavior of the custom element. | 342 On the Dart side, a class implements the behavior of the custom element. |
| 312 You associate the Dart class with the custom element using the `@CustomTag` | 343 You associate the Dart class with the custom element using the `@CustomTag` |
| 313 annotation and the name of the custom element. | 344 annotation and the name of the custom element. |
| 314 | 345 |
| 315 <img class="scale-img-max" src="images/polymer-element-definition.png" | 346 <img class="scale-img-max" src="images/polymer-element-definition.png" |
| 316 alt="Two parts to a custom element definition"> | 347 alt="Two parts to a custom element definition"> |
| 317 | 348 |
| 318 This diagram gives an overview of the TuteStopwatch class: | 349 This diagram gives an overview of the TuteStopwatch class: |
| 319 | 350 |
| 320 <img class="scale-img-max" src="images/dart-script-code.png" | 351 <img class="scale-img-max" src="images/dart-script-code.png" |
| 321 alt="The script code for the tute-stopwatch element"> | 352 alt="The script code for the tute-stopwatch element"> |
| 322 | 353 |
| 323 Any Dart class that backs a Polymer element must subclass PolymerElement. | 354 Classes that back Polymer elements are usually subclasses of PolymerElement, |
| 355 but they don't have to be. | |
| 356 They can extend any other HtmlElement subclass, | |
| 357 as long as they implement | |
| 358 <a href="https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/polymer /polymer.Polymer">Polymer</a> and | |
| 359 <a href="https://api.dartlang.org/observe/observe.Observable">Observable</a> (ty pically as mixins) | |
| 360 and provide a constructor named <code><em>CustomElement</em>.created()</code> | |
| 361 that invokes `super.created()` and | |
| 362 (if using Polymer as a mixin) `polymerCreated()`. | |
| 363 **{PENDING: true?}** | |
|
Siggi Cherem (dart-lang)
2014/05/28 19:00:21
Yes, the paragraph above seems accurate. A couple
Kathy Walrath
2014/05/29 21:14:15
Done.
| |
| 364 As an example, here's the code for PolymerElement: | |
| 365 | |
| 366 {% prettify dart %} | |
| 367 class PolymerElement extends HtmlElement with Polymer, Observable { | |
| 368 PolymerElement.created() : super.created() { | |
| 369 polymerCreated(); | |
| 370 } | |
| 371 } | |
| 372 {% endprettify %} | |
| 324 | 373 |
| 325 {% comment %} | 374 {% comment %} |
| 326 [xx: more about PolymerElement] | 375 [xx: more about PolymerElement] |
| 327 {% endcomment %} | 376 {% endcomment %} |
| 328 | 377 |
| 329 The class can respond to life-cycle milestones | 378 The class can respond to life-cycle milestones |
| 330 by overriding [life-cycle methods](#life-cycle-methods). | 379 by overriding [life-cycle methods](#life-cycle-methods). |
| 331 For example, the TuteStopwatch class overrides the `enteredView()` | 380 For example, the TuteStopwatch class overrides the `enteredView()` |
| 332 method—which is called when the element is inserted | 381 method—which is called when the element is inserted |
| 333 into the DOM—to initialize the app. | 382 into the DOM—to initialize the app. |
| 334 | 383 |
| 335 The `start()` method is an event handler for the **Start** button. | 384 The `start()` method is an event handler for the **Start** button. |
| 336 The event handler is declaratively connected to the button. | 385 The event handler is declaratively connected to the button. |
| 337 Refer to [Setting up event handlers declaratively](#event-handlers) to see how. | 386 Refer to [Setting up event handlers declaratively](#event-handlers) to see how. |
| 338 | 387 |
| 339 ##Overriding life-cycle methods {#life-cycle-methods} | 388 ##Overriding life-cycle methods {#life-cycle-methods} |
| 340 | 389 |
| 341 A custom element has four life-cycle methods | 390 A custom element has a constructor and three life-cycle methods |
| 342 that it can override: | 391 that it can override: |
| 343 | 392 |
| 344 |---|---| | 393 |---|---| |
| 345 | `created()` | Called when an instance of a custom element is created. | | 394 | <code><em>CustomElement</em>.created()</code> | The constructor used when crea ting an instance of a custom element. | |
| 346 | `enteredView()` | Called when an instance of a custom element is inserted into the DOM. | | 395 | `enteredView()` | Called when an instance of a custom element is inserted into the DOM. | |
|
Siggi Cherem (dart-lang)
2014/05/28 19:00:21
enteredView/leftView got renamed. They are still a
Kathy Walrath
2014/05/29 21:14:15
Done.
| |
| 347 | `leftView()` | Called when an instance of a custom element is removed from the DOM. | | 396 | `leftView()` | Called when an instance of a custom element is removed from the DOM. | |
| 348 | `attributeChanged()` | Called when an attribute, such as `class`, of an instan ce of the custom element is added, changed, or removed. | | 397 | `attributeChanged()` | Called when an attribute, such as `class`, of an instan ce of the custom element is added, changed, or removed. | |
| 349 {: .table} | 398 {: .table} |
| 350 | 399 |
| 351 You can override any of these life-cycle methods. | 400 You can implement the constructor, |
| 352 The overriding method | 401 if necessary, |
| 353 *must* call the super class method first. | 402 and override any of the life-cycle methods. |
| 403 The constructor or overriding method | |
| 404 *must* call the superclass constructor or method first. | |
| 354 | 405 |
| 355 The Stopwatch app overrides the `enteredView()` method because it | 406 The Stopwatch app overrides the `enteredView()` method because it |
| 356 needs a reference to each of the three buttons | 407 needs a reference to each of the three buttons |
| 357 so that it can enable and disable them. | 408 so that it can enable and disable them. |
| 358 When a tute-stopwatch custom element is inserted into the DOM | 409 When a tute-stopwatch custom element is inserted into the DOM |
| 359 the buttons have been created, so the references to them | 410 the buttons have been created, so the references to them |
| 360 will be available when the enteredView() method is called. | 411 will be available when the enteredView() method is called. |
| 361 | 412 |
| 362 {% prettify dart %} | 413 {% prettify dart %} |
| 363 void enteredView() { | 414 void enteredView() { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 406 | 457 |
| 407 You can use expressions within the double curly brackets. | 458 You can use expressions within the double curly brackets. |
| 408 <a href="http://pub.dartlang.org/packages/polymer_expressions" | 459 <a href="http://pub.dartlang.org/packages/polymer_expressions" |
| 409 target="_blank">Polymer expressions</a> | 460 target="_blank">Polymer expressions</a> |
| 410 provide the default syntax. Examples of allowable expressions include: | 461 provide the default syntax. Examples of allowable expressions include: |
| 411 | 462 |
| 412 |---|---| | 463 |---|---| |
| 413 | `{``{``myObject.aProperty}}` | Property access. | | 464 | `{``{``myObject.aProperty}}` | Property access. | |
| 414 | `{``{``!empty}}` | Operators, like the logical not operator. | | 465 | `{``{``!empty}}` | Operators, like the logical not operator. | |
| 415 | `{``{``myList[3]}}` | List indexing. | | 466 | `{``{``myList[3]}}` | List indexing. | |
| 416 | `{``{``myFilter()}}` | Data filtering. | | 467 | `{``{``myFilter()}}` | Data filtering. | |
|
Siggi Cherem (dart-lang)
2014/05/28 19:00:21
let's remove this line - the syntax changed to "ex
Kathy Walrath
2014/05/29 21:14:15
Done.
| |
| 417 {: .table} | 468 {: .table} |
| 418 | 469 |
| 419 ##Setting up event handlers declaratively {#event-handlers} | 470 ##Setting up event handlers declaratively {#event-handlers} |
| 420 | 471 |
| 421 This example has three buttons, each | 472 This example has three buttons, each |
| 422 with an event handler that is written in Dart, | 473 with an event handler that is written in Dart, |
| 423 but attached to the button declaratively from HTML. | 474 but attached to the button declaratively from HTML. |
| 424 | 475 |
| 425 <img class="scale-img-max" src="images/click-handler.png" | 476 <img class="scale-img-max" src="images/click-handler.png" |
| 426 alt="Declaratively attach a click handler"> | 477 alt="Declaratively attach a click handler"> |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 494 You can style elements within the custom element using | 545 You can style elements within the custom element using |
| 495 the appropriate selectors for those elements. | 546 the appropriate selectors for those elements. |
| 496 You don't need to worry about naming conflicts on the page. | 547 You don't need to worry about naming conflicts on the page. |
| 497 Any CSS selectors used within the <style> section | 548 Any CSS selectors used within the <style> section |
| 498 apply only to those elements within the template. | 549 apply only to those elements within the template. |
| 499 | 550 |
| 500 For further details about styling custom elements, | 551 For further details about styling custom elements, |
| 501 refer to | 552 refer to |
| 502 [A Guide to Styling Elements](http://www.polymer-project.org/articles/styling-el ements.html) | 553 [A Guide to Styling Elements](http://www.polymer-project.org/articles/styling-el ements.html) |
| 503 | 554 |
| 555 | |
| 556 ##Deploying an app that uses Polymer | |
| 557 | |
| 558 To convert your app to JavaScript | |
| 559 that you can deploy to the web, | |
| 560 you need to use the Polymer transformers. | |
| 561 You can test your app's JavaScript version using | |
| 562 either Dart Editor's **Run as JavaScript** command | |
| 563 or the `pub serve` command. | |
| 564 To produce deployable files, | |
| 565 use the `pub build` command. | |
| 566 | |
| 567 ###Specifying transformers | |
| 568 | |
| 569 Add a `transformers` entry to your app's `pubspec.yaml` file | |
| 570 to specify the Polymer transformers: | |
| 571 | |
| 572 {% prettify yaml %} | |
| 573 ... | |
| 574 dependencies: | |
| 575 polymer: ">=0.10.0 <0.11.0" | |
| 576 transformers: | |
| 577 - polymer | |
| 578 {% endprettify %} | |
| 579 | |
| 580 By default, Polymer assumes that all files under `web` can be | |
| 581 _entry points_—**{PENDING: define entry point here}**. | |
|
Siggi Cherem (dart-lang)
2014/05/28 19:00:21
That would be practically the equivalent of an app
Kathy Walrath
2014/05/29 21:14:15
What if you have a secondary page—one that you sho
Kathy Walrath
2014/05/29 21:14:15
Done.
Siggi Cherem (dart-lang)
2014/05/29 22:21:21
Yes - basically anything you'll serve up and expec
| |
| 582 You can use an `entry_points` field to limit the HTML files that | |
| 583 the Polymer transformers process. | |
| 584 For example: | |
| 585 | |
| 586 {% prettify yaml %} | |
| 587 ... | |
| 588 transformers: | |
| 589 - polymer: | |
| 590 entry_points: web/index.html | |
| 591 {% endprettify %} | |
| 592 | |
| 593 <aside class="alert alert-warning"> | |
| 594 <b>Important:</b> | |
| 595 YAML is whitespace-sensitive, | |
| 596 so take care to indent the <tty>entry_points</tty> field as shown. | |
| 597 </aside> | |
| 598 | |
| 599 For more information on using the Polymer transformers, see the | |
| 600 [Polymer package documentation](http://pub.dartlang.org/packages/polymer). | |
| 601 | |
| 602 ###Testing the JavaScript version | |
| 603 | |
| 604 If you're using Dart Editor, | |
| 605 you can test the JavaScript version in your default web browser | |
| 606 by right-clicking your app's main file | |
| 607 (for example, web/index.html) | |
| 608 and choosing **Run as JavaScript**. | |
|
Siggi Cherem (dart-lang)
2014/05/28 19:00:21
I just realized one thing. Starting in SDK 1.5 the
Kathy Walrath
2014/05/29 21:14:15
I'll leave out that detail for now.
Will running
Siggi Cherem (dart-lang)
2014/05/29 22:21:21
A little bit - the first launch is slower, subsequ
| |
| 609 A browser tab opens containing your running app. | |
| 610 You can copy the URL from that tab into any other browser | |
| 611 that you'd like to test. | |
| 612 | |
| 613 Alternatively, run `pub serve` | |
| 614 on the command line, | |
| 615 from the app's top directory. | |
| 616 That command runs the Polymer transformers on the app | |
| 617 and starts up an HTTP server to serve the app. | |
| 618 The command's output gives you a URL that you can | |
| 619 copy and paste into a browser window. | |
| 620 If you change the app's files and reload the window, | |
| 621 you see the updated version of the app. | |
| 622 | |
| 623 | |
| 624 ###Generating JavaScript files | |
| 625 | |
| 626 When you're ready to generate files, | |
| 627 run `pub build`—either at the command line or | |
| 628 using Dart Editor—to | |
| 629 generate the files your app needs | |
| 630 to run in any modern browser. | |
| 631 The generated files appear in the | |
| 632 `build` directory, alongside your `pubspec.yaml` file. | |
| 633 | |
| 634 | |
| 504 ##Other resources | 635 ##Other resources |
| 505 | 636 |
| 506 Use these other resources to learn more about Polymer: | 637 Use these other resources to learn more about Polymer: |
| 507 | 638 |
| 508 * The | 639 * The |
| 509 <a href="http://www.dartlang.org/polymer-dart/" | 640 <a href="http://www.dartlang.org/polymer-dart/" |
| 510 target="_blank">Polymer.dart</a> homepage provides information | 641 target="_blank">Polymer.dart</a> homepage provides information |
| 511 specific to the Dart port of the Polymer project. | 642 specific to the Dart port of the Polymer project. |
| 512 | 643 |
| 513 * The Polymer project website | 644 * The Polymer project website |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 530 | 661 |
| 531 The next tutorial, | 662 The next tutorial, |
| 532 [Fetch Data Dynamically](/docs/tutorials/fetchdata/), | 663 [Fetch Data Dynamically](/docs/tutorials/fetchdata/), |
| 533 shows you how to fetch data | 664 shows you how to fetch data |
| 534 and use JSON to encode and decode | 665 and use JSON to encode and decode |
| 535 that data. | 666 that data. |
| 536 | 667 |
| 537 {% endcapture %} | 668 {% endcapture %} |
| 538 | 669 |
| 539 {% include tutorial.html %} | 670 {% include tutorial.html %} |
| OLD | NEW |