1
0
mirror of https://github.com/troisjs/trois.git synced 2024-11-24 04:12:02 +08:00

bindObjectProps

This commit is contained in:
Kevin Levron 2021-10-31 22:06:34 +01:00
parent b377d0f1da
commit f109069d9f

View File

@ -1,4 +1,4 @@
import { toRef, watch, WatchStopHandle } from 'vue' import { ref, toRef, watch, WatchStopHandle } from 'vue'
type OptionSetter = (dst: any, key: string, value: any) => void type OptionSetter = (dst: any, key: string, value: any) => void
@ -27,6 +27,17 @@ export function bindObjectProp(
return watch(r, (value) => { applyObjectProps(dst, value, setter) }) return watch(r, (value) => { applyObjectProps(dst, value, setter) })
} }
export function bindObjectProps(
src: any,
dst: any,
apply = true,
setter?: OptionSetter
): WatchStopHandle {
if (apply) applyObjectProps(dst, src, setter)
const r = ref(src)
return watch(r, (value) => { applyObjectProps(dst, value, setter) }, { deep: true })
}
export function setFromProp(o: Record<string, unknown>, prop: Record<string, unknown>): void { export function setFromProp(o: Record<string, unknown>, prop: Record<string, unknown>): void {
if (prop instanceof Object) { if (prop instanceof Object) {
Object.entries(prop).forEach(([key, value]) => { Object.entries(prop).forEach(([key, value]) => {