Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Add WatchPaths decorator #342

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/vue-property-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,34 @@ export function Watch(path: string, options: WatchOptions = {}) {
})
}

/**
* decorator of a watch function for elements of an array
* @param paths the paths or the expression to observe
* @param WatchOption
* @return MethodDecorator
*/
export function WatchPaths(paths: string[], options: WatchOptions = {}) {
const { deep = false, immediate = false } = options;

return createDecorator((componentOptions, handler) => {
if(typeof componentOptions.watch !== 'object') {
componentOptions.watch = Object.create(null);
}

const watch: any = componentOptions.watch;

paths.forEach(path => {
if(typeof watch[path] === 'object' && !Array.isArray(watch[path])){
watch[path] = [watch[path]];
}else if(typeof watch[path] === 'undefined') {
watch[path] = [];
}

watch[path].push({handler, deep, immediate});
});
});
}

// Code copied from Vue/src/shared/util.js
const hyphenateRE = /\B([A-Z])/g
const hyphenate = (str: string) => str.replace(hyphenateRE, '-$1').toLowerCase()
Expand Down