| OLD | NEW |
| 1 --- | 1 --- |
| 2 layout: default | 2 layout: default |
| 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: fetchdata | 8 next: fetchdata |
| 9 next-title: "Fetch Data Dynamically" | 9 next-title: "Fetch Data Dynamically" |
| 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.8.1<
/a>. | 52 <a href="https://pub.dartlang.org/packages/polymer#versions">polymer.dart 0.8.7<
/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. |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 If you are using command line tools, | 176 If you are using command line tools, |
| 177 you can run it with the command `pub install`. | 177 you can run it with the command `pub install`. |
| 178 | 178 |
| 179 ##Including Polymer.dart in your application {#bootstrap} | 179 ##Including Polymer.dart in your application {#bootstrap} |
| 180 | 180 |
| 181 To use Polymer.dart features such as custom elements, | 181 To use Polymer.dart features such as custom elements, |
| 182 you need to include Polymer in both | 182 you need to include Polymer in both |
| 183 the HTML side and the Dart side of your app. | 183 the HTML side and the Dart side of your app. |
| 184 | 184 |
| 185 * In the primary HTML file for your app, | 185 * In the primary HTML file for your app, |
| 186 include `packages/polymer/boot.js` | 186 import `package:polymer/init.dart` within a <script> tag |
| 187 in the <head> section. | 187 in the <head> section. |
| 188 When using this script, | 188 This script contains the `main()` function |
| 189 you do not need to define a top-level `main()` function, | 189 for the app and initializes Polymer. |
| 190 although you can. | 190 |
| 191 **Note:** Use this script instead of `packages/browser/dart.js`. | 191  |
| 192 |
| 193 * In the primary HTML file for your app, |
| 194 include the `packages/browser/dart.js` bootstrap script |
| 195 in the <head> section. |
| 192 | 196 |
| 193  | 197  |
| 194 | 198 |
| 195 * In your Dart code, import the Polymer library: | 199 * In your Dart code, import the Polymer library: |
| 196 | 200 |
| 197  | 201  |
| 198 | 202 |
| 199 ##Instantiating a custom element {#instantiating} | 203 ##Instantiating a custom element {#instantiating} |
| 200 | 204 |
| 201 To create an instance of a custom element, | 205 To create an instance of a custom element, |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307  | 311  |
| 308 | 312 |
| 309 Any Dart class that backs a Polymer element must subclass PolymerElement. | 313 Any Dart class that backs a Polymer element must subclass PolymerElement. |
| 310 | 314 |
| 311 {% comment %} | 315 {% comment %} |
| 312 [xx: more about PolymerElement] | 316 [xx: more about PolymerElement] |
| 313 {% endcomment %} | 317 {% endcomment %} |
| 314 | 318 |
| 315 The class can respond to life-cycle milestones | 319 The class can respond to life-cycle milestones |
| 316 by overriding [life-cycle methods](#life-cycle-methods). | 320 by overriding [life-cycle methods](#life-cycle-methods). |
| 317 For example, the TuteStopwatch class overrides the `inserted()` | 321 For example, the TuteStopwatch class overrides the `enteredView()` |
| 318 method—which is called when the element is inserted | 322 method—which is called when the element is inserted |
| 319 into the DOM—to initialize the app. | 323 into the DOM—to initialize the app. |
| 320 | 324 |
| 321 The `start()` method is an event handler for the **Start** button. | 325 The `start()` method is an event handler for the **Start** button. |
| 322 The event handler is declaratively connected to the button. | 326 The event handler is declaratively connected to the button. |
| 323 Refer to [Setting up event handlers declaratively](#event-handlers) to see how. | 327 Refer to [Setting up event handlers declaratively](#event-handlers) to see how. |
| 324 | 328 |
| 325 ##Overriding life-cycle methods {#life-cycle-methods} | 329 ##Overriding life-cycle methods {#life-cycle-methods} |
| 326 | 330 |
| 327 A custom element has four life-cycle methods | 331 A custom element has four life-cycle methods |
| 328 that it can override: | 332 that it can override: |
| 329 | 333 |
| 330 |---|---| | 334 |---|---| |
| 331 | `created()` | Called when an instance of a custom element is created. | | 335 | `created()` | Called when an instance of a custom element is created. | |
| 332 | `inserted()` | Called when an instance of a custom element is inserted into th
e DOM. | | 336 | `enteredView()` | Called when an instance of a custom element is inserted into
the DOM. | |
| 333 | `removed()` | Called when an instance of a custom element is removed from the
DOM. | | 337 | `leftView()` | Called when an instance of a custom element is removed from the
DOM. | |
| 334 | `attributeChanged()` | Called when an attribute, such as `class`, of an instan
ce of the custom element is added, changed, or removed. | | 338 | `attributeChanged()` | Called when an attribute, such as `class`, of an instan
ce of the custom element is added, changed, or removed. | |
| 335 {: .table} | 339 {: .table} |
| 336 | 340 |
| 337 You can override any of these life-cycle methods. | 341 You can override any of these life-cycle methods. |
| 338 The overriding method | 342 The overriding method |
| 339 *must* call the super class method first. | 343 *must* call the super class method first. |
| 340 | 344 |
| 341 The Stopwatch app overrides the `inserted()` method because it | 345 The Stopwatch app overrides the `enteredView()` method because it |
| 342 needs a reference to each of the three buttons | 346 needs a reference to each of the three buttons |
| 343 so that it can enable and disable them. | 347 so that it can enable and disable them. |
| 344 When a tute-stopwatch custom element is inserted into the DOM | 348 When a tute-stopwatch custom element is inserted into the DOM |
| 345 the buttons have been created, so the references to them | 349 the buttons have been created, so the references to them |
| 346 will be available when the inserted() method is called. | 350 will be available when the enteredView() method is called. |
| 347 | 351 |
| 348 {% prettify dart %} | 352 {% prettify dart %} |
| 349 void inserted() { | 353 void enteredView() { |
| 350 super.inserted(); | 354 super.enteredView(); |
| 351 startButton = $['startButton']; | 355 startButton = $['startButton']; |
| 352 stopButton = $['stopButton']; | 356 stopButton = $['stopButton']; |
| 353 resetButton = $['resetButton']; | 357 resetButton = $['resetButton']; |
| 354 | 358 |
| 355 stopButton.disabled = true; | 359 stopButton.disabled = true; |
| 356 resetButton.disabled = true; | 360 resetButton.disabled = true; |
| 357 } | 361 } |
| 358 {% endprettify %} | 362 {% endprettify %} |
| 359 | 363 |
| 360 The code uses _automatic node finding_, a Polymer feature, | 364 The code uses _automatic node finding_, a Polymer feature, |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 | 516 |
| 513 The next tutorial, | 517 The next tutorial, |
| 514 [Fetch Data Dynamically](/docs/tutorials/fetchdata/), | 518 [Fetch Data Dynamically](/docs/tutorials/fetchdata/), |
| 515 shows you how to fetch data | 519 shows you how to fetch data |
| 516 and use JSON to encode and decode | 520 and use JSON to encode and decode |
| 517 that data. | 521 that data. |
| 518 | 522 |
| 519 {% endcapture %} | 523 {% endcapture %} |
| 520 | 524 |
| 521 {% include tutorial.html %} | 525 {% include tutorial.html %} |
| OLD | NEW |