`useVModelObject`
See original GitHub issueThis is a new function proposal.
This function is a helps implementing v-model="obj"
.
Usage
<template>
<div>
<input v-model="a" type="text" />
<input v-model="b" type="text" />
</div>
</template>
<script lang="ts">
import { defineComponent, PropType, watch } from 'vue'
import { useVModelObject } from './useVModelObject'
export default defineComponent({
props: {
modelValue: {
type: Object as PropType<{ a: string, b: string }>,
required: true
}
},
setup(props) {
const data = useVModelObject(props)
return { ...data }
}
})
</script>
Sample implementation
import { computed, getCurrentInstance, WritableComputedRef } from 'vue'
export const useVModelObject = <P, K extends keyof P>(
props: P,
key = 'modelValue' as K,
emit?: (name: string, ...args: any[]) => void
): { [KK in keyof P[K]]: WritableComputedRef<P[K][KK]> } => {
const vm = getCurrentInstance()
const _emit = emit ?? vm!.emit
const computedObject: any = {}
for (const k of Object.keys(props[key])) {
computedObject[k] = computed({
get() {
return (props[key] as any)[k]
},
set(v) {
_emit(`update:${key}`, { ...props[key], [k]: v })
}
})
}
return computedObject
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Model object - Apple Developer
A model object is a type of object that contains the data of an application, provides access to that data, and implements logic...
Read more >Spring Web MVC – The Model Object - Apps Developer Blog
A Model is an object that you can use to pass attributes from the Controller to the View. To make the Model object...
Read more >The Basics of the Object Model - Section.io
An object model uses various diagrams to show how objects behave and perform real-world tasks. The diagrams used include use-case diagram ...
Read more >Defining model objects
A model object is a Java object that represents, or models, an item in the application domain. This example uses a Switch data...
Read more >Model Objects - Java Practices
The term Model Object is an informal term, with no widely accepted definition. Here, Model Objects (MOs) refer to data-centric classes which encapsulate ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Extending
toRefs
sounds great. I will make a PR.I really like the idea.
The “toNestedRefs” naming seem pretty clear to me.
I really love the idea of “extending” the
toRefs()
function too.