question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`useVModelObject`

See original GitHub issue

This 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:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
sapphi-redcommented, Apr 18, 2021

Extending toRefs sounds great. I will make a PR.

0reactions
Tahulcommented, Apr 12, 2021

I really like the idea.

The “toNestedRefs” naming seem pretty clear to me.

I really love the idea of “extending” the toRefs() function too.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found