Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(watch): allow handler to be a string #1775

Merged
merged 3 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor(watch): consistent with string watcher
  • Loading branch information
posva committed Aug 4, 2020
commit 6f6b0ded16a2f0c4286c89f0263cf358ed9733c5
8 changes: 7 additions & 1 deletion packages/runtime-core/__tests__/apiOptions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,10 @@ describe('api: options', () => {
test('Expected a function as watch handler', () => {
const Comp = {
watch: {
foo: 'notExistingMethod'
foo: 'notExistingMethod',
foo2: {
handler: 'notExistingMethod2'
}
},
render() {}
}
Expand All @@ -729,6 +732,9 @@ describe('api: options', () => {
expect(
'Invalid watch handler specified by key "notExistingMethod"'
).toHaveBeenWarned()
expect(
'Invalid watch handler specified by key "notExistingMethod2"'
).toHaveBeenWarned()
})

test('Invalid watch option', () => {
Expand Down
12 changes: 8 additions & 4 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,10 +704,14 @@ function createWatcher(
if (isArray(raw)) {
raw.forEach(r => createWatcher(r, ctx, publicThis, key))
} else {
const handler = isString(raw.handler)
? (ctx[raw.handler] as WatchCallback)
: raw.handler
watch(getter, handler.bind(publicThis), raw)
const handler = isFunction(raw.handler)
? raw.handler.bind(publicThis)
: (ctx[raw.handler] as WatchCallback)
if (isFunction(handler)) {
watch(getter, handler, raw)
} else if (__DEV__) {
warn(`Invalid watch handler specified by key "${raw}"`, handler)
}
}
} else if (__DEV__) {
warn(`Invalid watch option: "${key}"`)
Expand Down