Chromium Code Reviews| 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. |
| 63 (Note: Polymer supersedes Web UI.) | 63 (Note: Polymer supersedes Web UI.) |
| 64 </aside> | 64 </aside> |
| 65 | 65 |
| 66 * [An example](#an-example) | 66 * [An example](#an-example) |
| 67 * [Installing Polymer.dart](#getting-polymer-dart) | 67 * [Installing Polymer.dart](#getting-polymer-dart) |
| 68 * [Including Polymer.dart in your application](#bootstrap) | 68 * [Including Polymer.dart in your application](#bootstrap) |
| 69 * [Initializing Polymer](#initializing-polymer) | |
| 69 * [Instantiating a custom element](#instantiating) | 70 * [Instantiating a custom element](#instantiating) |
| 70 * [Defining a custom element](#define-element) | 71 * [Defining a custom element](#define-element) |
| 71 * [Providing a template for the custom element](#providing-a-template) | 72 * [Providing a template for the custom element](#providing-a-template) |
| 72 * [Providing a script for the custom element](#providing-a-script) | 73 * [Providing a script for the custom element](#providing-a-script) |
| 73 * [Overiding life-cycle methods](#life-cycle-methods) | 74 * [Overiding life-cycle methods](#life-cycle-methods) |
| 74 * [Using data binding](#data-binding) | 75 * [Using data binding](#data-binding) |
| 75 * [Setting up event handlers declaratively](#event-handlers) | 76 * [Setting up event handlers declaratively](#event-handlers) |
| 76 * [Styling a custom element](#scoped-css) | 77 * [Styling a custom element](#scoped-css) |
| 77 * [Other resources](#other-resources) | 78 * [Other resources](#other-resources) |
| 78 * [What next?](#what-next) | 79 * [What next?](#what-next) |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 If you are using command line tools, | 177 If you are using command line tools, |
| 177 you can run it with the command `pub install`. | 178 you can run it with the command `pub install`. |
| 178 | 179 |
| 179 ##Including Polymer.dart in your application {#bootstrap} | 180 ##Including Polymer.dart in your application {#bootstrap} |
| 180 | 181 |
| 181 To use Polymer.dart features such as custom elements, | 182 To use Polymer.dart features such as custom elements, |
| 182 you need to include Polymer in both | 183 you need to include Polymer in both |
| 183 the HTML side and the Dart side of your app. | 184 the HTML side and the Dart side of your app. |
| 184 | 185 |
| 185 * In the primary HTML file for your app, | 186 * In the primary HTML file for your app, |
| 186 include `packages/polymer/boot.js` | 187 include the `packages/browser/dart.js` bootstrap script |
| 187 in the <head> section. | 188 in the <head> section. |
| 188 When using this script, | |
| 189 you do not need to define a top-level `main()` function, | |
| 190 although you can. | |
| 191 **Note:** Use this script instead of `packages/browser/dart.js`. | |
| 192 | 189 |
| 193  | 190  |
| 194 | 191 |
| 195 * In your Dart code, import the Polymer library: | 192 * In your Dart code, import the Polymer library: |
| 196 | 193 |
| 197  | 194  |
| 198 | 195 |
| 196 ##Initializing Polymer {#initializing-polymer} | |
| 197 | |
| 198 The stopwatch example has a `main()` function | |
| 199 that calls `initPolymer()` to initialize Polymer. | |
| 200 The main function is included as an inline Dart script in the HTML file. | |
|
Kathy Walrath
2013/10/29 21:48:48
I had a little trouble parsing this sentence. Incl
| |
| 201 | |
| 202 {% prettify html %} | |
| 203 ... | |
| 204 <script type="application/dart"> | |
| 205 import 'package:polymer/polymer.dart'; | |
| 206 | |
| 207 void main() { | |
| 208 initPolymer(); | |
| 209 } | |
| 210 </script> | |
| 211 ... | |
| 212 {% endprettify %} | |
| 213 | |
| 199 ##Instantiating a custom element {#instantiating} | 214 ##Instantiating a custom element {#instantiating} |
| 200 | 215 |
| 201 To create an instance of a custom element, | 216 To create an instance of a custom element, |
| 202 use the name of the custom element just as you would any normal HTML tag. | 217 use the name of the custom element just as you would any normal HTML tag. |
| 203 In this example, the tag name is `tute-stopwatch`. | 218 In this example, the tag name is `tute-stopwatch`. |
| 204 | 219 |
| 205  | 220  |
| 206 | 221 |
| 207 Using best practices, | 222 Using best practices, |
| 208 the custom element definition is in a separate file. | 223 the custom element definition is in a separate file. |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 307  | 322  |
| 308 | 323 |
| 309 Any Dart class that backs a Polymer element must subclass PolymerElement. | 324 Any Dart class that backs a Polymer element must subclass PolymerElement. |
| 310 | 325 |
| 311 {% comment %} | 326 {% comment %} |
| 312 [xx: more about PolymerElement] | 327 [xx: more about PolymerElement] |
| 313 {% endcomment %} | 328 {% endcomment %} |
| 314 | 329 |
| 315 The class can respond to life-cycle milestones | 330 The class can respond to life-cycle milestones |
| 316 by overriding [life-cycle methods](#life-cycle-methods). | 331 by overriding [life-cycle methods](#life-cycle-methods). |
| 317 For example, the TuteStopwatch class overrides the `inserted()` | 332 For example, the TuteStopwatch class overrides the `enteredView()` |
| 318 method—which is called when the element is inserted | 333 method—which is called when the element is inserted |
| 319 into the DOM—to initialize the app. | 334 into the DOM—to initialize the app. |
| 320 | 335 |
| 321 The `start()` method is an event handler for the **Start** button. | 336 The `start()` method is an event handler for the **Start** button. |
| 322 The event handler is declaratively connected to the button. | 337 The event handler is declaratively connected to the button. |
| 323 Refer to [Setting up event handlers declaratively](#event-handlers) to see how. | 338 Refer to [Setting up event handlers declaratively](#event-handlers) to see how. |
| 324 | 339 |
| 325 ##Overriding life-cycle methods {#life-cycle-methods} | 340 ##Overriding life-cycle methods {#life-cycle-methods} |
| 326 | 341 |
| 327 A custom element has four life-cycle methods | 342 A custom element has four life-cycle methods |
| 328 that it can override: | 343 that it can override: |
| 329 | 344 |
| 330 |---|---| | 345 |---|---| |
| 331 | `created()` | Called when an instance of a custom element is created. | | 346 | `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. | | 347 | `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. | | 348 | `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. | | 349 | `attributeChanged()` | Called when an attribute, such as `class`, of an instan ce of the custom element is added, changed, or removed. | |
| 335 {: .table} | 350 {: .table} |
| 336 | 351 |
| 337 You can override any of these life-cycle methods. | 352 You can override any of these life-cycle methods. |
| 338 The overriding method | 353 The overriding method |
| 339 *must* call the super class method first. | 354 *must* call the super class method first. |
| 340 | 355 |
| 341 The Stopwatch app overrides the `inserted()` method because it | 356 The Stopwatch app overrides the `enteredView()` method because it |
| 342 needs a reference to each of the three buttons | 357 needs a reference to each of the three buttons |
| 343 so that it can enable and disable them. | 358 so that it can enable and disable them. |
| 344 When a tute-stopwatch custom element is inserted into the DOM | 359 When a tute-stopwatch custom element is inserted into the DOM |
| 345 the buttons have been created, so the references to them | 360 the buttons have been created, so the references to them |
| 346 will be available when the inserted() method is called. | 361 will be available when the enteredView() method is called. |
| 347 | 362 |
| 348 {% prettify dart %} | 363 {% prettify dart %} |
| 349 void inserted() { | 364 void enteredView() { |
| 350 super.inserted(); | 365 super.enteredView(); |
| 351 startButton = $['startButton']; | 366 startButton = $['startButton']; |
| 352 stopButton = $['stopButton']; | 367 stopButton = $['stopButton']; |
| 353 resetButton = $['resetButton']; | 368 resetButton = $['resetButton']; |
| 354 | 369 |
| 355 stopButton.disabled = true; | 370 stopButton.disabled = true; |
| 356 resetButton.disabled = true; | 371 resetButton.disabled = true; |
| 357 } | 372 } |
| 358 {% endprettify %} | 373 {% endprettify %} |
| 359 | 374 |
| 360 The code uses _automatic node finding_, a Polymer feature, | 375 The code uses _automatic node finding_, a Polymer feature, |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 512 | 527 |
| 513 The next tutorial, | 528 The next tutorial, |
| 514 [Fetch Data Dynamically](/docs/tutorials/fetchdata/), | 529 [Fetch Data Dynamically](/docs/tutorials/fetchdata/), |
| 515 shows you how to fetch data | 530 shows you how to fetch data |
| 516 and use JSON to encode and decode | 531 and use JSON to encode and decode |
| 517 that data. | 532 that data. |
| 518 | 533 |
| 519 {% endcapture %} | 534 {% endcapture %} |
| 520 | 535 |
| 521 {% include tutorial.html %} | 536 {% include tutorial.html %} |
| OLD | NEW |