2019-05-29 13:43:46 +08:00
|
|
|
import { ComponentInstance } from './component'
|
|
|
|
|
|
|
|
export const RenderProxyHandlers = {
|
|
|
|
get(target: ComponentInstance, key: string) {
|
|
|
|
const { state, props } = target
|
|
|
|
if (state.hasOwnProperty(key)) {
|
2019-05-29 17:36:53 +08:00
|
|
|
return state[key]
|
2019-05-29 13:43:46 +08:00
|
|
|
} else if (props.hasOwnProperty(key)) {
|
|
|
|
return props[key]
|
|
|
|
} else {
|
|
|
|
switch (key) {
|
|
|
|
case '$state':
|
|
|
|
return target.state
|
|
|
|
case '$props':
|
|
|
|
return target.props
|
|
|
|
case '$attrs':
|
|
|
|
return target.attrs
|
|
|
|
case '$slots':
|
|
|
|
return target.slots
|
|
|
|
case '$refs':
|
|
|
|
return target.refs
|
2019-06-03 13:44:45 +08:00
|
|
|
case '$parent':
|
|
|
|
return target.parent
|
|
|
|
case '$root':
|
|
|
|
return target.root
|
|
|
|
case '$el':
|
|
|
|
return target.vnode && target.vnode.el
|
2019-06-19 16:43:34 +08:00
|
|
|
case '$emit':
|
|
|
|
return target.emit
|
2019-05-29 13:43:46 +08:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
set(target: ComponentInstance, key: string, value: any): boolean {
|
|
|
|
const { state } = target
|
|
|
|
if (state.hasOwnProperty(key)) {
|
2019-05-29 17:36:53 +08:00
|
|
|
state[key] = value
|
2019-05-29 13:43:46 +08:00
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
if (__DEV__) {
|
|
|
|
if (key[0] === '$') {
|
|
|
|
// TODO warn attempt of mutating public property
|
|
|
|
} else if (target.props.hasOwnProperty(key)) {
|
|
|
|
// TODO warn attempt of mutating prop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|