From e6d4b3788bdf897b2f89c5ff696daab8c1bbb13c Mon Sep 17 00:00:00 2001 From: Arthur Evans Date: Thu, 10 Jul 2014 13:46:14 -0700 Subject: [PATCH 1/6] Add inferred tag name --- docs/polymer/polymer.md | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/docs/polymer/polymer.md b/docs/polymer/polymer.md index 491a0a12e8..68c533e2b2 100644 --- a/docs/polymer/polymer.md +++ b/docs/polymer/polymer.md @@ -23,6 +23,21 @@ At the heart of {{site.project_title}} are [Custom Elements](/platform/custom-el +The `Polymer` method registers the element. If the first argument to `Polymer` is a string, it specifies +the tag name being registered. You can omit this argument if both of the following are true: + +* The `Polymer` call is made in an inline script inside the `` tag. +* The element is defined in an HTML import, not in the main document. + +For example, assuming that `tag-name` is defined in an HTML import, its definition can be simplified to: + + + + + + ### Attributes {{site.project_title}} reserves special attributes to be used on ``: @@ -104,6 +119,11 @@ which calls `Polymer('tag-name')`: +**Important:** If the script comes before or after the element definition, +the first argument to `Polymer` must be the tag name. +{: .alert .alert-error } + + #### Imperative registration {#imperativeregister} Elements can be registered in pure JavaScript like so: @@ -129,9 +149,8 @@ Custom Elements polyfill picks it up. ### Adding public properties and methods {#propertiesmethods} -If you wish to define methods/properties on your element (optional), pass an object -as the second argument to `Polymer()`. This object is used to define -the element's `prototype`. +To define methods/properties on your element, pass an object argument to `Polymer()`. +This object is used to define the element's `prototype`. The following example defines a property `message`, a property `greeting` using an ES5 getter, and a method `foo`: @@ -152,6 +171,12 @@ using an ES5 getter, and a method `foo`: **Note:** `this` references the custom element itself inside a {{site.project_title}} element. For example, `this.localName == 'tag-name'`. {: .alert .alert-info } +A custom element's prototype is assembled in this order: + +- The base prototype is copied from the DOM object that the custom element extends (by default, `HTMLElement`). +- {{site.project_title}} adds a set of built-in methods and properties (see [Built-in element methods](#builtin)). +- The properties and methods defined in the proto-object are added into the element's prototype. + **Important:** Be careful when initializing properties that are objects or arrays. Due to the nature of `prototype`, you may run into unexpected "shared state" across instances of the same element. If you're initializing an array or object, do it in the `created` callback rather than directly on the `prototype`. // Good! From 4a076676dcbecd1f388c2d6198c3e403ba1b6b04 Mon Sep 17 00:00:00 2001 From: Arthur Evans Date: Mon, 14 Jul 2014 12:58:03 -0700 Subject: [PATCH 2/6] Move inferred tag-name to its own section. More tweaks to "adding properties and methods" section. --- docs/polymer/polymer.md | 81 ++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 30 deletions(-) diff --git a/docs/polymer/polymer.md b/docs/polymer/polymer.md index 68c533e2b2..93f5731acf 100644 --- a/docs/polymer/polymer.md +++ b/docs/polymer/polymer.md @@ -23,20 +23,6 @@ At the heart of {{site.project_title}} are [Custom Elements](/platform/custom-el -The `Polymer` method registers the element. If the first argument to `Polymer` is a string, it specifies -the tag name being registered. You can omit this argument if both of the following are true: - -* The `Polymer` call is made in an inline script inside the `` tag. -* The element is defined in an HTML import, not in the main document. - -For example, assuming that `tag-name` is defined in an HTML import, its definition can be simplified to: - - - - - ### Attributes @@ -94,8 +80,18 @@ This also means that any of the below examples will also work: + + ### Alternate ways to register an element {#altregistration} +There are several alternative ways to register elements: + +- [Separating script from markup](#separatescript). +- [Inferring the tag name](#inferredtagname) to eliminate extra boilerplate. +- [Registering imperatively](#imperativeregister) using JavaScript. + +#### Separate script from markup {#separatescript} + For convenient decoupling of script and markup, you don't have to inline your script. {{site.project_title}} elements can be created by referencing an external script which calls `Polymer('tag-name')`: @@ -119,10 +115,27 @@ which calls `Polymer('tag-name')`: -**Important:** If the script comes before or after the element definition, -the first argument to `Polymer` must be the tag name. -{: .alert .alert-error } +#### Inferred tag name {#inferredtagname} + +If the first argument to `Polymer` is a string, it specifies +the tag name being registered. +You can _omit_ the tag name argument if **both of the following are true**: + +* The `Polymer` call is made **in an inline script** inside the `` tag. +* The element is defined **in an HTML import**, not in the main document. + +For example, assuming that `tag-name` is defined in an HTML import, its definition can be simplified to: + + + + + + +If {{site.project_title}} can't infer the tag name, it logs a warning to the console, and element +registration fails. #### Imperative registration {#imperativeregister} @@ -149,8 +162,13 @@ Custom Elements polyfill picks it up. ### Adding public properties and methods {#propertiesmethods} -To define methods/properties on your element, pass an object argument to `Polymer()`. -This object is used to define the element's `prototype`. +To define methods/properties on your element, pass an object argument to `Polymer()`: + +
+Polymer(tag-name, proto-object);
+
+ +Where both _tag-name_ and _proto-object_ are optional. The _proto-object_ is used to define the element's `prototype`. The following example defines a property `message`, a property `greeting` using an ES5 getter, and a method `foo`: @@ -175,7 +193,10 @@ A custom element's prototype is assembled in this order: - The base prototype is copied from the DOM object that the custom element extends (by default, `HTMLElement`). - {{site.project_title}} adds a set of built-in methods and properties (see [Built-in element methods](#builtin)). -- The properties and methods defined in the proto-object are added into the element's prototype. +- The properties and methods defined in the _proto-object_ are added into the element's prototype. + +Avoid defining a property or method with the same name as a native DOM property or method, such as `id`, `children` or +`focus`; the results are unpredictable. **Important:** Be careful when initializing properties that are objects or arrays. Due to the nature of `prototype`, you may run into unexpected "shared state" across instances of the same element. If you're initializing an array or object, do it in the `created` callback rather than directly on the `prototype`. @@ -363,7 +384,7 @@ As an example, here's an element that publishes three public properties, `foo`, @@ -371,7 +392,7 @@ And here's one using the `publish` object: @@ -417,7 +438,7 @@ You can provide your own default values by explicitly specifying the default val @@ -665,7 +686,7 @@ It uses special on-event syntax to trigger this binding be -If {{site.project_title}} can't infer the tag name, it logs a warning to the console, and element -registration fails. - #### Imperative registration {#imperativeregister} Elements can be registered in pure JavaScript like so: @@ -195,8 +192,8 @@ A custom element's prototype is assembled in this order: - {{site.project_title}} adds a set of built-in methods and properties (see [Built-in element methods](#builtin)). - The properties and methods defined in the _proto-object_ are added into the element's prototype. -Avoid defining a property or method with the same name as a native DOM property or method, such as `id`, `children` or -`focus`; the results are unpredictable. +Avoid defining a property or method with the same name as a native DOM property or method, such as `id`, `children`, +`focus`, `title` and `hidden`; the results are unpredictable. **Important:** Be careful when initializing properties that are objects or arrays. Due to the nature of `prototype`, you may run into unexpected "shared state" across instances of the same element. If you're initializing an array or object, do it in the `created` callback rather than directly on the `prototype`. @@ -392,7 +389,7 @@ And here's one using the `publish` object: @@ -438,7 +435,7 @@ You can provide your own default values by explicitly specifying the default val @@ -686,7 +683,7 @@ It uses special on-event syntax to trigger this binding be + +The element declaration includes: + +- The `name` attribute specifies the name of the new custom element. +- The optional ` @@ -911,7 +945,7 @@ and data-bound.
@@ -927,7 +961,7 @@ When you override an inherited method, you can call the parent's method with `th You are {{praise}} !