feat: enable returning observable state from hooks()

This commit is contained in:
Evan You 2018-10-30 00:33:45 -04:00
parent 6767bf83c4
commit a17c377be0
2 changed files with 17 additions and 11 deletions

View File

@ -74,6 +74,10 @@ const renderProxyHandlers = {
if ((i = target._rawData) !== null && i.hasOwnProperty(key)) { if ((i = target._rawData) !== null && i.hasOwnProperty(key)) {
target.$data[key] = value target.$data[key] = value
return true return true
} else if ((i = target._hookProps) !== null && i.hasOwnProperty(key)) {
// this enables returning observable objects from hooks()
i[key] = value
return true
} else { } else {
return Reflect.set(target, key, value, receiver) return Reflect.set(target, key, value, receiver)
} }

View File

@ -105,22 +105,24 @@ export let isRendering = false
export function renderInstanceRoot(instance: ComponentInstance): VNode { export function renderInstanceRoot(instance: ComponentInstance): VNode {
let vnode let vnode
const {
$options: { hooks },
render,
$proxy,
$props,
$slots,
$attrs,
$parentVNode
} = instance
try { try {
setCurrentInstance(instance) setCurrentInstance(instance)
if (instance.hooks) { if (hooks) {
instance._hookProps = instance._hookProps = hooks.call($proxy, $props) || null
instance.hooks.call(instance.$proxy, instance.$props) || null
} }
if (__DEV__) { if (__DEV__) {
isRendering = true isRendering = true
} }
vnode = instance.render.call( vnode = render.call($proxy, $props, $slots, $attrs, $parentVNode)
instance.$proxy,
instance.$props,
instance.$slots,
instance.$attrs,
instance.$parentVNode
)
if (__DEV__) { if (__DEV__) {
isRendering = false isRendering = false
} }
@ -128,7 +130,7 @@ export function renderInstanceRoot(instance: ComponentInstance): VNode {
} catch (err) { } catch (err) {
handleError(err, instance, ErrorTypes.RENDER) handleError(err, instance, ErrorTypes.RENDER)
} }
return normalizeComponentRoot(vnode, instance.$parentVNode) return normalizeComponentRoot(vnode, $parentVNode)
} }
export function renderFunctionalRoot(vnode: VNode): VNode { export function renderFunctionalRoot(vnode: VNode): VNode {