-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add useModelMigration composable
Signed-off-by: Grigorii K. Shartsev <[email protected]>
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import { getCurrentInstance, computed } from 'vue' | ||
|
||
/** | ||
* Create model proxy to new v9 model (modelValue + update:modelValue) with a fallback to old model | ||
* @param {string} oldModelName - Name of model prop in nextcloud-vue v8 | ||
* @param {string} oldModelEvent - Event name of model event in nextcloud-vue v8 | ||
* @return {import('vue').WritableComputedRef} - model proxy | ||
*/ | ||
export function useModelMigration(oldModelName, oldModelEvent) { | ||
const vm = getCurrentInstance()!.proxy | ||
|
||
const model = computed({ | ||
get() { | ||
if (vm.$props[oldModelName] !== undefined) { | ||
return vm.$props[oldModelName] | ||
} | ||
return vm.$props.modelValue | ||
}, | ||
|
||
set(value) { | ||
vm.$emit('update:modelValue', value) | ||
vm.$emit(oldModelEvent, value) | ||
} | ||
}) | ||
|
||
return model | ||
} |