Polymer / old-docs-site

Old Polymer site. Replaced by these repos: polymer-project.org, polymer-library-docs
https://www.polymer-project.org
1.02k stars 2.44k forks source link

What about Function type in properties #1962

Open jpradelle opened 7 years ago

jpradelle commented 7 years ago

In release note of v1.3.0 there is a sample code using "type: Function" in properties definition https://www.polymer-project.org/1.0/docs/release-notes#v1-3-0-dynamic-fns

translationService: {
  type: Function,
  value: function() {
    return function(str){ return str + 'ish'; };
  }
}

But in documentation there is nothing about Function type. In declare properties page https://www.polymer-project.org/2.0/docs/devguide/properties :

Type: constructor (one of Boolean, Date, Number, String, Array or Object)

And nothing about Function in the documentation. Maybe a doc chapter is missing, or Function type should be avoided and might not be supported in future versions ?

More generally custom serialization is not clear:

To supply custom serialization for a custom element, override your element's serialize method.

And nothing about deserialization

In PropertyAccessors mixin source code it seems we can implement any type we want and override serialize and deserialize, but as this is not in polymer-project.org doc, will it be maintained in future releases ?

      /**
       * Converts a typed JavaScript value to a string.
       *
       * This method is called by Polymer when setting JS property values to
       * HTML attributes.  Users may override this method on Polymer element
       * prototypes to provide serialization for custom types.
       *
       * @method _serializeValue
       * @param {*} value Property value to serialize.
       * @return {string | undefined} String serialized from the provided property value.
       */
      _serializeValue(value)

      /**
       * Converts a string to a typed JavaScript value.
       *
       * This method is called by Polymer when reading HTML attribute values to
       * JS properties.  Users may override this method on Polymer element
       * prototypes to provide deserialization for custom `type`s.  Note,
       * the `type` argument is the value of the `type` field provided in the
       * `properties` configuration object for a given property, and is
       * by convention the constructor for the type to deserialize.
       *
       * Note: The return value of `undefined` is used as a sentinel value to
       * indicate the attribute should be removed.
       *
       * @method _deserializeValue
       * @param {string} value Attribute value to deserialize.
       * @param {*} type Type to deserialize the string to.
       * @return {*} Typed value deserialized from the provided string.
       */
      _deserializeValue(value, type)
web-padawan commented 7 years ago

In fact, we can pass callbacks to child elements by defining them like this:

filterCallback: {
    type: Object,
    value: function () {
        return function (item) {
            return item.active;
        }.bind(this);
    }
},

And then we just need to pass this property through one-way binding. The child element will be able to call this callback to filter items.

ghost commented 7 years ago

@arthurevans any ideas for people on the Polymer team who can help answer this?

justinfagnani commented 7 years ago

@katejeffreys I'll try to add some detail here...

Type: constructor (one of Boolean, Date, Number, String, Array or Object)

is a little misleading. Those are they types which Polymer understands how to serialize/deserialize. You can still provide another type, like Function for documentation, and to opt out of serialization (since Polymer won't know what to do). If you then want to enable serialization, you do so via _serializevalue.

In practice though, serializing non-primitive values is rare and it's fine to leave that out, in which case the type is mostly documentation.

arthurevans commented 7 years ago

Should add doc for custom serializing/deserializing. And add upgrade guide note about serialize=>_serializeValue etc.