v2.0.0-pre.18
useClone
The useClone utility makes working with form data easier. It provides the following features in a fairly smart way:
- Simple access to automatic patch diffing for
patchrequests. Only send the data to the server that has actually changed. - The clone and commit pattern runs under the hood, keeping the number of store actions to a minimum.
- Eager UI Updates: the UI updates immediately and assumes requests will be successful. Any failures are rolled back.
FeathersModel Required
It currently only works with FeathersModel instances because it depends on the .save() and .patch() instance methods.
Basic Example
Here's an example form that uses useClone on a user prop.
vue
<template>
<form @submit.prevent="() => clone.save()">
<input
v-model="clones.name"
type="text"
placeholder="Enter the User's Name"
>
<button type="submit">
Save
</button>
</form>
</template>
<script setup lang="ts">
import { useClone } from 'feathers-pinia'
const props = defineProps({
user: { type: Object },
})
const clone = useClone(props, 'user')
</script>
API
useClone(props, propName, options)
props {Object}a component'spropsobject (or areactivewith model instances at the top level). RequiredpropName {String}the name of the prop to watch for incoming Model instances. Requiredoptions {Object}an options objectuseExisting {boolean}whentruetellsuseCloneto reuse any existing clone. This is handy for handling two instances ofuseCloneat the same time on the record. Default:false.deep {boolean}whentrue, tellsuseCloneto re-clone any time the original record changes.
The value returned from useClone will change based on the value of props[propName]:
- If the prop holds a Model instance, it returns the cloned instance.
- If the prop does not hold a Model instance, it returns
null.
Note that the clone object in the previous example is a ref, so you access it with .value outside the template.
Diffing Data for Patch
Patch diffing is now built into the core of Feathers-Pinia. The .save() method of every cloned FeathersModel instance will perform diffing for you.
See the Automatic Patch Diffing API.