| OLD | NEW |
| (Empty) | |
| 1 <!-- |
| 2 @license |
| 3 Copyright (c) 2017 The Polymer Project Authors. All rights reserved. |
| 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 7 Code distributed by Google as part of the polymer project is also |
| 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 9 --> |
| 10 |
| 11 <link rel="import" href="../utils/templatize.html"> |
| 12 |
| 13 <script> |
| 14 (function() { |
| 15 'use strict'; |
| 16 |
| 17 /** |
| 18 * The `Polymer.Templatizer` behavior adds methods to generate instances of |
| 19 * templates that are each managed by an anonymous `Polymer.PropertyEffects` |
| 20 * instance where data-bindings in the stamped template content are bound to |
| 21 * accessors on itself. |
| 22 * |
| 23 * This behavior is provided in Polymer 2.x as a hybrid-element convenience |
| 24 * only. For non-hybrid usage, the `Polymer.Templatize` library |
| 25 * should be used instead. |
| 26 * |
| 27 * Example: |
| 28 * |
| 29 * // Get a template from somewhere, e.g. light DOM |
| 30 * let template = this.querySelector('template'); |
| 31 * // Prepare the template |
| 32 * this.templatize(template); |
| 33 * // Instance the template with an initial data model |
| 34 * let instance = this.stamp({myProp: 'initial'}); |
| 35 * // Insert the instance's DOM somewhere, e.g. light DOM |
| 36 * Polymer.dom(this).appendChild(instance.root); |
| 37 * // Changing a property on the instance will propagate to bindings |
| 38 * // in the template |
| 39 * instance.myProp = 'new value'; |
| 40 * |
| 41 * Users of `Templatizer` may need to implement the following abstract |
| 42 * API's to determine how properties and paths from the host should be |
| 43 * forwarded into to instances: |
| 44 * |
| 45 * _forwardHostPropV2: function(prop, value) |
| 46 * |
| 47 * Likewise, users may implement these additional abstract API's to determin
e |
| 48 * how instance-specific properties that change on the instance should be |
| 49 * forwarded out to the host, if necessary. |
| 50 * |
| 51 * _notifyInstancePropV2: function(inst, prop, value) |
| 52 * |
| 53 * In order to determine which properties are instance-specific and require |
| 54 * custom notification via `_notifyInstanceProp`, define an `_instanceProps` |
| 55 * object containing keys for each instance prop, for example: |
| 56 * |
| 57 * _instanceProps: { |
| 58 * item: true, |
| 59 * index: true |
| 60 * } |
| 61 * |
| 62 * Any properties used in the template that are not defined in _instanceProp |
| 63 * will be forwarded out to the Templatize `owner` automatically. |
| 64 * |
| 65 * Users may also implement the following abstract function to show or |
| 66 * hide any DOM generated using `stamp`: |
| 67 * |
| 68 * _showHideChildren: function(shouldHide) |
| 69 * |
| 70 * Note that some callbacks are suffixed with `V2` in the Polymer 2.x behavi
or |
| 71 * as the implementations will need to differ from the callbacks required |
| 72 * by the 1.x Templatizer API due to changes in the `TemplateInstance` API |
| 73 * between versions 1.x and 2.x. |
| 74 * |
| 75 * @polymerBehavior |
| 76 * @memberof Polymer |
| 77 */ |
| 78 let Templatizer = { |
| 79 |
| 80 /** |
| 81 * Generates an anonymous `TemplateInstance` class (stored as `this.ctor`) |
| 82 * for the provided template. This method should be called once per |
| 83 * template to prepare an element for stamping the template, followed |
| 84 * by `stamp` to create new instances of the template. |
| 85 * |
| 86 * @param {HTMLTemplateElement} template Template to prepare |
| 87 * @param {boolean=} mutableData When `true`, the generated class will ski
p |
| 88 * strict dirty-checking for objects and arrays (always consider them to |
| 89 * be "dirty"). Defaults to false. |
| 90 */ |
| 91 templatize(template, mutableData) { |
| 92 this._templatizerTemplate = template; |
| 93 this.ctor = Polymer.Templatize.templatize(template, this, { |
| 94 mutableData: Boolean(mutableData), |
| 95 parentModel: this._parentModel, |
| 96 instanceProps: this._instanceProps, |
| 97 forwardHostProp: this._forwardHostPropV2, |
| 98 notifyInstanceProp: this._notifyInstancePropV2 |
| 99 }); |
| 100 }, |
| 101 |
| 102 /** |
| 103 * Creates an instance of the template prepared by `templatize`. The obje
ct |
| 104 * returned is an instance of the anonymous class generated by `templatize
` |
| 105 * whose `root` property is a document fragment containing newly cloned |
| 106 * template content, and which has property accessors corresponding to |
| 107 * properties referenced in template bindings. |
| 108 * |
| 109 * @param {Object=} model Object containing initial property values to |
| 110 * populate into the template bindings. |
| 111 * @return {TemplateInstanceBase} Returns the created instance of |
| 112 * the template prepared by `templatize`. |
| 113 */ |
| 114 stamp(model) { |
| 115 return new this.ctor(model); |
| 116 }, |
| 117 |
| 118 /** |
| 119 * Returns the template "model" (`TemplateInstance`) associated with |
| 120 * a given element, which serves as the binding scope for the template |
| 121 * instance the element is contained in. A template model should be used |
| 122 * to manipulate data associated with this template instance. |
| 123 * |
| 124 * @param {HTMLElement} el Element for which to return a template model. |
| 125 * @return {TemplateInstanceBase} Model representing the binding scope for |
| 126 * the element. |
| 127 */ |
| 128 modelForElement(el) { |
| 129 return Polymer.Templatize.modelForElement(this._templatizerTemplate, el)
; |
| 130 } |
| 131 }; |
| 132 |
| 133 Polymer.Templatizer = Templatizer; |
| 134 |
| 135 })(); |
| 136 </script> |
| OLD | NEW |