Skip to content

prop "parse" function #107

@mesqueeb

Description

@mesqueeb

I think it would save a lot of boilerplate code if we could somehow transform props with a simple function defined right in the props configuration.

Eg. a function called parse could be executed before props are passed to setup(props) and before the props are registered on this

export default {
  props: {
    name: {
      type: String,
      parse: val => titleCase(val)
    },
    skills: {
      type: Object,
      // merge onto default values
      parse: val => Object.assign({power: 9000}, val)
    },
  },
}

The current implementation is:

export default {
  props: {
    name: String,
    skills: Object,
  },
  data () {
    const nameParsed = titleCase(this.name)
    const skillsParsed = Object.assign({power: 9000}, this.skills)
    return { nameParsed, skillsParsed }
  },
  watch: {
    name (val) {
      this.name = titleCase(val)
    },
    skills (val) {
      this.skills = Object.assign({power: 9000}, val)
    },
  },
}

The main problems are:

  • name clashing forces us to use a convention like nameParsed
  • we need to set up additional watchers, just for simple parsing of props
  • we need to decide if it's not better to do all this as computed property or not (i honestly don't know the trade-offs between computed and data + watch)

A simple parse function to be defined in the prop config seems like a very valuable addition. It's easy to understand and not breaking; so backwards compatible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions