Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit b426d8f

Browse files
author
Arthur Evans
committed
Update to reflect new support for inferred tag name.
1 parent 8ea0164 commit b426d8f

File tree

1 file changed

+83
-49
lines changed

1 file changed

+83
-49
lines changed

docs/polymer/polymer.md

+83-49
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,20 @@ At the heart of {{site.project_title}} are [Custom Elements](/platform/custom-el
2020
<template>
2121
<!-- shadow DOM here -->
2222
</template>
23-
<script>Polymer('tag-name');</script>
23+
<script>
24+
Polymer({
25+
// properties and methods here
26+
});
27+
</script>
2428
</polymer-element>
2529

30+
The element declaration includes:
31+
32+
- The `name` attribute specifies the name of the new custom element.
33+
- The optional `<template>` tag defines HTML content that is
34+
cloned into the shadow DOM of each instance of the element.
35+
- The `Polymer` method, which _registers_ the element, so it's
36+
recognized as a custom element by the browser.
2637

2738
### Attributes
2839

@@ -42,7 +53,7 @@ At the heart of {{site.project_title}} are [Custom Elements](/platform/custom-el
4253
<td><code>extends</code></td><td>optional</td><td>Used to <a href="#extending-other-elements">extend other elements</a>.</td>
4354
</tr>
4455
<tr>
45-
<td><code>noscript</code></td><td>optional</td><td>For simple elements that don't need to call <code>Polymer()</code>. See <a href="#altregistration">Alternate ways to register an element</a>.</td>
56+
<td><code>noscript</code></td><td>optional</td><td>For simple elements that don't need to call <code>Polymer()</code>. See <a href="#altregistration">Element registration</a>.</td>
4657
</tr>
4758
<tr>
4859
<td><code>constructor</code></td><td>optional</td><td>The name of the constructor to put on the global object. Allows users to create instances of your element using the <code>new</code> operator (e.g. <code>var tagName = new TagName()</code>).</td>
@@ -56,7 +67,7 @@ on each instance of the element. For example:
5667

5768
<polymer-element name="tag-name" class="active" mycustomattr>
5869
<template>...</template>
59-
<script>Polymer('tag-name');</script>
70+
<script>Polymer();</script>
6071
</polymer-element>
6172

6273
When an instance of `<tag-name>` is created, it contains `class="active" mycustomattr`
@@ -82,19 +93,41 @@ This also means that any of the below examples will also work:
8293

8394

8495

85-
### Alternate ways to register an element {#altregistration}
96+
### Element registration {#altregistration}
97+
98+
The `Polymer` method is used to register an element:
99+
100+
<pre>
101+
Polymer(<em class="nocode">tag-name</em>, <em class="nocode">proto-object</em>);
102+
</pre>
103+
104+
Where:
105+
106+
* _tag-name_ matches the `name` attribute in the `<polymer-element>` tag.
107+
_tag-name_ is **optional** as long as **the `<script>` tag that calls `Polymer`
108+
is placed inside the `<polymer-element>` tag.**
109+
110+
* _proto-object_ defines properties and methods to add to the
111+
elements prototype. See [Adding public properties and methods](#propertiesmethods).
112+
_proto-object_ is always optional.
113+
114+
The simplest way to invoke `Polymer` is to place an inline script inside
115+
your `<polymer-element>` tag:
116+
117+
<polymer-element name="simple-tag">
118+
<script>Polymer();</script>
119+
</polymer-element>
86120

87121
There are several alternative ways to register elements:
88122

89123
- [Separating script from markup](#separatescript).
90-
- [Inferring the tag name](#inferredtagname) to eliminate extra boilerplate.
91124
- [Registering imperatively](#imperativeregister) using JavaScript.
92125

93126
#### Separate script from markup {#separatescript}
94127

95128
For convenient decoupling of script and markup, you don't have to inline your script.
96129
{{site.project_title}} elements can be created by referencing an external script
97-
which calls `Polymer('tag-name')`:
130+
which calls `Polymer`:
98131

99132
<!-- 1. Script referenced inside the element definition. -->
100133
<polymer-element name="tag-name">
@@ -108,35 +141,31 @@ which calls `Polymer('tag-name')`:
108141
<template>...</template>
109142
</polymer-element>
110143

144+
In case #2, where the script is invoked before the `<polymer-element>` tag,
145+
the call to `Polymer` **must include the tag name**:
146+
147+
// tagname.js
148+
Polymer('tag-name', ... );
149+
150+
For elements that don't require custom properties or methods, you can
151+
use the `noscript` attribute:
152+
111153
<!-- 3. No script -->
112154
<polymer-element name="tag-name" constructor="TagName" noscript>
113155
<template>
114156
<!-- shadow DOM here -->
115157
</template>
116158
</polymer-element>
117159

118-
#### Inferred tag name {#inferredtagname}
160+
The `noscript` attribute is equivalent to including:
119161

120-
If the first argument to `Polymer` is a string, it specifies
121-
the tag name being registered.
122-
123-
You can _omit_ the tag name argument if **both of the following are true**:
124-
125-
* The `Polymer` call is made **in an inline script** inside the `<polymer-element>` tag.
126-
* The element is defined **in an HTML import**, not in the main document.
127-
128-
For example, assuming that `tag-name` is defined in an HTML import, its definition can be simplified to:
129-
130-
<polymer-element name="tag-name" constructor="TagName">
131-
<template>
132-
<!-- shadow DOM here -->
133-
</template>
134-
<script>Polymer();</script>
135-
</polymer-element>
162+
<script>
163+
Polymer();
164+
</script>
136165

137166
#### Imperative registration {#imperativeregister}
138167

139-
Elements can be registered in pure JavaScript like so:
168+
Elements can be registered in pure JavaScript like this:
140169

141170
<script>
142171
Polymer('name-tag', {nameColor: 'red'});
@@ -154,26 +183,30 @@ Elements can be registered in pure JavaScript like so:
154183

155184
<name-tag name="John"></name-tag>
156185

157-
Note that you need to add the `<polymer-element>` to the document so that the
186+
You need to add the `<polymer-element>` to the document so that the
158187
Custom Elements polyfill picks it up.
159188

189+
**Important:** Since the `Polymer` call here is outside the `<polymer-element>`,
190+
it must include the tag name argument.
191+
{: .alert .alert-error }
192+
160193
### Adding public properties and methods {#propertiesmethods}
161194

162195
To define methods/properties on your element, pass an object argument to `Polymer()`:
163196

164197
<pre>
165-
Polymer(<em class="nocode">tag-name</em>, <em class="nocode">proto-object</em>);
198+
Polymer([ <em class="nocode">tag-name</em>, ] <em class="nocode">proto-object</em>);
166199
</pre>
167200

168-
Where both _tag-name_ and _proto-object_ are optional. The _proto-object_ is used to define the element's `prototype`.
201+
The _proto-object_ contains properties and methods to add to the element's `prototype`.
169202

170203
The following example defines a property `message`, a property `greeting`
171204
using an ES5 getter, and a method `foo`:
172205

173206
<polymer-element name="tag-name">
174207
<template>{{greeting}}</template>
175208
<script>
176-
Polymer('tag-name', {
209+
Polymer({
177210
message: "Hello!",
178211
get greeting() {
179212
return this.message + ' there!';
@@ -196,6 +229,7 @@ Avoid defining a property or method with the same name as a native DOM property
196229
`focus`, `title` and `hidden`; the results are unpredictable.
197230

198231
**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`.
232+
{: .alert .alert-error }
199233

200234
// Good!
201235
Polymer('x-foo', {
@@ -220,11 +254,11 @@ techniques like anonymous self-calling functions:
220254
<template>...</template>
221255
<script>
222256
(function() {
223-
// Ran once. Private and static to the element.
257+
// Run once. Private and static to the element.
224258
var foo_ = new Foo();
225259

226-
// Ran for every instance of the element that's created.
227-
Polymer('tag-name', {
260+
// Run for every instance of the element that's created.
261+
Polymer({
228262
get foo() { return foo_; }
229263
});
230264
})();
@@ -245,7 +279,7 @@ To achieve this, you can use the [MonoState Pattern](http://c2.com/cgi/wiki?Mono
245279
var firstName = 'John';
246280
var lastName = 'Smith';
247281

248-
Polymer('app-globals', {
282+
Polymer({
249283
ready: function() {
250284
this.firstName = firstName;
251285
this.lastName = lastName;
@@ -264,7 +298,7 @@ Then use the element as you would any other, and data-bind it to a property that
264298
<div id="lastname">{{globals.lastName}}</div>
265299
</template>
266300
<script>
267-
Polymer('my-component', {
301+
Polymer({
268302
ready: function() { this.globals = this.$.globals; }
269303
});
270304
</script>
@@ -277,7 +311,7 @@ A slight tweak of this approach lets you configure the value of the globals exte
277311
(function() {
278312
var values = {};
279313

280-
Polymer('app-globals', {
314+
Polymer({
281315
ready: function() {
282316
this.values = values;
283317
for (var i = 0; i < this.attributes.length; ++i) {
@@ -389,7 +423,7 @@ And here's one using the `publish` object:
389423

390424
<polymer-element name="x-foo">
391425
<script>
392-
Polymer('x-foo', {
426+
Polymer({
393427
publish: {
394428
foo: 'I am foo!',
395429
bar: 5,
@@ -425,7 +459,7 @@ By default, properties defined in `attributes` are initialized to `null`:
425459
<polymer-element name="x-foo" attributes="foo">
426460
<script>
427461
// x-foo has a foo property with default value of null.
428-
Polymer('x-foo');
462+
Polymer();
429463
</script>
430464
</polymer-element>
431465

@@ -435,7 +469,7 @@ You can provide your own default values by explicitly specifying the default val
435469

436470
<polymer-element name="x-foo" attributes="bar">
437471
<script>
438-
Polymer('x-foo', {
472+
Polymer({
439473
// x-foo has a bar property with default value false.
440474
bar: false
441475
});
@@ -446,7 +480,7 @@ Or you can define the whole thing using the `publish` property:
446480

447481
<polymer-element name="x-foo">
448482
<script>
449-
Polymer('x-foo', {
483+
Polymer({
450484
publish: {
451485
bar: false
452486
}
@@ -460,7 +494,7 @@ created for each instance of the element:
460494

461495
<polymer-element name="x-default" attributes="settings">
462496
<script>
463-
Polymer('x-default', {
497+
Polymer({
464498
created: function() {
465499
// create a default settings object for this instance
466500
this.settings = {
@@ -521,7 +555,7 @@ includes an unlikely combination of all three:
521555

522556
<polymer-element name="hint-element" attributes="isReady items">
523557
<script>
524-
Polymer('hint-element', {
558+
Polymer({
525559

526560
// hint that isReady is a Boolean
527561
isReady: false,
@@ -592,7 +626,7 @@ For example:
592626

593627
<polymer-element name="disappearing-element">
594628
<script>
595-
Polymer('disappearing-element', {
629+
Polymer({
596630
publish: {
597631
hidden: {
598632
value: false,
@@ -632,7 +666,7 @@ For example, we can define a `name-tag` element that publishes two properties,
632666
Hello! My name is <span style="color:{%raw%}{{nameColor}}{%endraw%}">{%raw%}{{name}}{%endraw%}</span>
633667
</template>
634668
<script>
635-
Polymer('name-tag', {
669+
Polymer({
636670
nameColor: "orange"
637671
});
638672
</script>
@@ -683,7 +717,7 @@ It uses special <code>on-<em>event</em></code> syntax to trigger this binding be
683717
<button on-click="{% raw %}{{buttonClick}}{% endraw %}"></button>
684718
</template>
685719
<script>
686-
Polymer('g-cool', {
720+
Polymer({
687721
keypressHandler: function(event, detail, sender) { ...},
688722
buttonClick: function(event, detail, sender) { ... }
689723
});
@@ -826,7 +860,7 @@ For example, the following defines a component whose template contains an `<inpu
826860
<input type="text" id="nameInput">
827861
</template>
828862
<script>
829-
Polymer('x-form', {
863+
Polymer({
830864
logNameValue: function() {
831865
console.log(this.$.nameInput.value);
832866
}
@@ -866,7 +900,7 @@ Example:
866900
<button on-click="{{onClick}}">Send hurt</button>
867901
</template>
868902
<script>
869-
Polymer('ouch-button', {
903+
Polymer({
870904
onClick: function() {
871905
this.fire('ouch', {msg: 'That hurt!'}); // fire(inType, inDetail, inToNode)
872906
}
@@ -898,7 +932,7 @@ and data-bound.
898932
You are {%raw%}{{praise}}{%endraw%} <content></content>!
899933
</template>
900934
<script>
901-
Polymer('polymer-cool', {
935+
Polymer({
902936
praise: 'cool'
903937
});
904938
</script>
@@ -911,7 +945,7 @@ and data-bound.
911945
<shadow></shadow> <!-- "You are cool Matt" -->
912946
</template>
913947
<script>
914-
Polymer('polymer-cooler');
948+
Polymer();
915949
</script>
916950
</polymer-element>
917951

@@ -927,7 +961,7 @@ When you override an inherited method, you can call the parent's method with `th
927961
You are {{praise}} <content></content>!
928962
</template>
929963
<script>
930-
Polymer('polymer-cool', {
964+
Polymer({
931965
praise: 'cool',
932966
makeCoolest: function() {
933967
this.praise = 'the coolest';
@@ -942,7 +976,7 @@ When you override an inherited method, you can call the parent's method with `th
942976
<shadow></shadow>
943977
</template>
944978
<script>
945-
Polymer('polymer-cooler', {
979+
Polymer({
946980
praise: 'cool',
947981
makeCoolest: function() {
948982
this.super(); // calls polymer-cool's makeCoolest()

0 commit comments

Comments
 (0)