feat(runtime-dom): v-model directive runtime

This commit is contained in:
Evan You
2019-10-11 17:55:20 -04:00
parent a371b2ec0e
commit a42ad6cc9d
5 changed files with 262 additions and 47 deletions

View File

@@ -47,6 +47,24 @@ const reset = () => {
}
const getNow = () => cachedNow || (p.then(reset), (cachedNow = _getNow()))
export function addEventListener(
el: Element,
event: string,
handler: EventListener,
options?: EventListenerOptions
) {
el.addEventListener(event, handler, options)
}
export function removeEventListener(
el: Element,
event: string,
handler: EventListener,
options?: EventListenerOptions
) {
el.removeEventListener(event, handler, options)
}
export function patchEvent(
el: Element,
name: string,
@@ -71,12 +89,12 @@ export function patchEvent(
prev.once !== next.once
) {
if (invoker) {
el.removeEventListener(name, invoker as any, prevOptions as any)
removeEventListener(el, name, invoker, prev)
}
if (nextValue && value) {
const invoker = createInvoker(value, instance)
nextValue.invoker = invoker
el.addEventListener(name, invoker, nextOptions as any)
addEventListener(el, name, invoker, next)
}
return
}
@@ -89,14 +107,15 @@ export function patchEvent(
nextValue.invoker = invoker
invoker.lastUpdated = getNow()
} else {
el.addEventListener(
addEventListener(
el,
name,
createInvoker(value, instance),
nextOptions as any
nextOptions || void 0
)
}
} else if (invoker) {
el.removeEventListener(name, invoker, prevOptions as any)
removeEventListener(el, name, invoker, prevOptions || void 0)
}
}

View File

@@ -13,5 +13,10 @@ export function patchDOMProp(
if ((key === 'innerHTML' || key === 'textContent') && prevChildren != null) {
unmountChildren(prevChildren, parentComponent, parentSuspense)
}
if (key === 'value' && el.tagName !== 'PROGRESS') {
// store value as _value as well since
// non-string values will be stringified.
el._value = value
}
el[key] = value == null ? '' : value
}