-
Notifications
You must be signed in to change notification settings - Fork 541
Open
Description
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
anddata
+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.
mika76, Grafikart, chriscalo, faisalhakim47, desislavsd and 3 moremika76, chriscalo, faisalhakim47, desislavsd and ellepereira
Metadata
Metadata
Assignees
Labels
No labels