|
92 | 92 |
|
93 | 93 | _takeAttributesToModel: function(model) {
|
94 | 94 | for (var i=0, l=this.attributes.length; i<l; i++) {
|
95 |
| - this.setAttributeToProperty(model, this.attributes[i].name); |
| 95 | + this._setAttributeToProperty(model, this.attributes[i].name); |
96 | 96 | }
|
97 | 97 | },
|
98 | 98 |
|
99 |
| - setAttributeToProperty: function(model, attrName) { |
| 99 | + _setAttributeToProperty: function(model, attrName) { |
100 | 100 | // Don't deserialize back to property if currently reflecting
|
101 | 101 | if (!this._serializing) {
|
102 | 102 | var propName = Polymer.CaseMap.dashToCamelCase(attrName);
|
|
111 | 111 |
|
112 | 112 | _serializing: false,
|
113 | 113 |
|
| 114 | + /** |
| 115 | + * Serializes a property to its associated attribute. |
| 116 | + * |
| 117 | + * Generally users should set `reflectToAttribute: true` in the |
| 118 | + * `properties` configuration to achieve automatic attribute reflection. |
| 119 | + * |
| 120 | + * @method reflectPropertyToAttribute |
| 121 | + * @param {string} name Property name to reflect. |
| 122 | + */ |
114 | 123 | reflectPropertyToAttribute: function(name) {
|
115 | 124 | this._serializing = true;
|
116 | 125 | this.serializeValueToAttribute(this[name],
|
117 | 126 | Polymer.CaseMap.camelToDashCase(name));
|
118 | 127 | this._serializing = false;
|
119 | 128 | },
|
120 | 129 |
|
| 130 | + /** |
| 131 | + * Sets a typed value to an HTML attribute on a node. |
| 132 | + * |
| 133 | + * This method calls the `serialize` method to convert the typed |
| 134 | + * value to a string. If the `serialize` method returns `undefined`, |
| 135 | + * the attribute will be removed (this is the default for boolean |
| 136 | + * type `false`). |
| 137 | + * |
| 138 | + * @method serializeValueToAttribute |
| 139 | + * @param {*} value Value to serialize. |
| 140 | + * @param {string} attribute Attribute name to serialize to. |
| 141 | + * @param {Element=} node Element to set attribute to (defaults to this). |
| 142 | + */ |
121 | 143 | serializeValueToAttribute: function(value, attribute, node) {
|
122 | 144 | var str = this.serialize(value);
|
| 145 | + // TODO(kschaaf): Consider enabling under a flag |
| 146 | + // if (str && str.length > 250) { |
| 147 | + // this._warn(this._logf('serializeValueToAttribute', |
| 148 | + // 'serializing long attribute values can lead to poor performance', this)); |
| 149 | + // } |
123 | 150 | (node || this)
|
124 | 151 | [str === undefined ? 'removeAttribute' : 'setAttribute']
|
125 | 152 | (attribute, str);
|
126 | 153 | },
|
127 | 154 |
|
| 155 | + /** |
| 156 | + * Converts a string to a typed value. |
| 157 | + * |
| 158 | + * This method is called by Polymer when reading HTML attribute values to |
| 159 | + * JS properties. Users may override this method on Polymer element |
| 160 | + * prototypes to provide deserialization for custom `type`s. Note, |
| 161 | + * the `type` argument is the value of the `type` field provided in the |
| 162 | + * `properties` configuration object for a given property, and is |
| 163 | + * by convention the constructor for the type to deserialize. |
| 164 | + * |
| 165 | + * Note: The return value of `undefined` is used as a sentinel value to |
| 166 | + * indicate the attribute should be removed. |
| 167 | + * |
| 168 | + * @method deserialize |
| 169 | + * @param {string} value Attribute value to deserialize. |
| 170 | + * @param {*} type Type to deserialize the string to. |
| 171 | + * @return {*} Typed value deserialized from the provided string. |
| 172 | + */ |
128 | 173 | deserialize: function(value, type) {
|
129 | 174 | switch (type) {
|
130 | 175 | case Number:
|
|
163 | 208 | return value;
|
164 | 209 | },
|
165 | 210 |
|
| 211 | + /** |
| 212 | + * Converts a typed value to a string. |
| 213 | + * |
| 214 | + * This method is called by Polymer when setting JS property values to |
| 215 | + * HTML attributes. Users may override this method on Polymer element |
| 216 | + * prototypes to provide serialization for custom types. |
| 217 | + * |
| 218 | + * @method serialize |
| 219 | + * @param {*} value Property value to serialize. |
| 220 | + * @return {string} String serialized from the provided property value. |
| 221 | + */ |
166 | 222 | serialize: function(value) {
|
167 | 223 | switch (typeof value) {
|
168 | 224 | case 'boolean':
|
|
0 commit comments