Skip to content

Commit 9b3ca2c

Browse files
authored
Merge pull request #5089 from MajorBreakfast/patch-3
Update docs: templatize() cannot be called multiple times
2 parents aa385bc + 27fc21c commit 9b3ca2c

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

lib/utils/templatize.html

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,11 @@
462462
* from `instance.parentModel` in cases where template instance nesting
463463
* causes an inner model to shadow an outer model.
464464
*
465+
* All callbacks are called bound to the `owner`. Any context
466+
* needed for the callbacks (such as references to `instances` stamped)
467+
* should be stored on the `owner` such that they can be retrieved via
468+
* `this`.
469+
*
465470
* When `options.forwardHostProp` is declared as an option, any properties
466471
* referenced in the template will be automatically forwarded from the host of
467472
* the `<template>` to instances, with the exception of any properties listed in
@@ -471,14 +476,18 @@
471476
* always be set to the instance (regardless of whether they would normally
472477
* be forwarded from the host).
473478
*
474-
* Note that the class returned from `templatize` is generated only once
475-
* for a given `<template>` using `options` from the first call for that
476-
* template, and the cached class is returned for all subsequent calls to
477-
* `templatize` for that template. As such, `options` callbacks should not
478-
* close over owner-specific properties since only the first `options` is
479-
* used; rather, callbacks are called bound to the `owner`, and so context
480-
* needed from the callbacks (such as references to `instances` stamped)
481-
* should be stored on the `owner` such that they can be retrieved via `this`.
479+
* Note that `templatize()` can be run only once for a given `<template>`.
480+
* Further calls will result in an error. Also, there is a special
481+
* behavior if the template was duplicated through a mechanism such as
482+
* `<dom-repeat>` or `<test-fixture>`. In this case, all calls to
483+
* `templatize()` return the same class for all duplicates of a template.
484+
* The class returned from `templatize()` is generated only once using
485+
* the `options` from the first call. This means that any `options`
486+
* provided to subsequent calls will be ignored. Therefore, it is very
487+
* important not to close over any variables inside the callbacks. Also,
488+
* arrow functions must be avoided because they bind the outer `this`.
489+
* Inside the callbacks, any contextual information can be accessed
490+
* through `this`, which points to the `owner`.
482491
*
483492
* @memberof Polymer.Templatize
484493
* @param {!HTMLTemplateElement} template Template to templatize

types/lib/utils/templatize.d.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ declare namespace Polymer {
129129
* from `instance.parentModel` in cases where template instance nesting
130130
* causes an inner model to shadow an outer model.
131131
*
132+
* All callbacks are called bound to the `owner`. Any context
133+
* needed for the callbacks (such as references to `instances` stamped)
134+
* should be stored on the `owner` such that they can be retrieved via
135+
* `this`.
136+
*
132137
* When `options.forwardHostProp` is declared as an option, any properties
133138
* referenced in the template will be automatically forwarded from the host of
134139
* the `<template>` to instances, with the exception of any properties listed in
@@ -138,14 +143,18 @@ declare namespace Polymer {
138143
* always be set to the instance (regardless of whether they would normally
139144
* be forwarded from the host).
140145
*
141-
* Note that the class returned from `templatize` is generated only once
142-
* for a given `<template>` using `options` from the first call for that
143-
* template, and the cached class is returned for all subsequent calls to
144-
* `templatize` for that template. As such, `options` callbacks should not
145-
* close over owner-specific properties since only the first `options` is
146-
* used; rather, callbacks are called bound to the `owner`, and so context
147-
* needed from the callbacks (such as references to `instances` stamped)
148-
* should be stored on the `owner` such that they can be retrieved via `this`.
146+
* Note that `templatize()` can be run only once for a given `<template>`.
147+
* Further calls will result in an error. Also, there is a special
148+
* behavior if the template was duplicated through a mechanism such as
149+
* `<dom-repeat>` or `<test-fixture>`. In this case, all calls to
150+
* `templatize()` return the same class for all duplicates of a template.
151+
* The class returned from `templatize()` is generated only once using
152+
* the `options` from the first call. This means that any `options`
153+
* provided to subsequent calls will be ignored. Therefore, it is very
154+
* important not to close over any variables inside the callbacks. Also,
155+
* arrow functions must be avoided because they bind the outer `this`.
156+
* Inside the callbacks, any contextual information can be accessed
157+
* through `this`, which points to the `owner`.
149158
*
150159
* @returns Generated class bound to the template
151160
* provided

0 commit comments

Comments
 (0)