22
33// https://github.com/Hanks10100/weex-native-directive/tree/master/component
44
5- import { mergeOptions } from 'core/util/index'
5+ import { mergeOptions , isPlainObject , noop } from 'core/util/index'
6+ import Watcher from 'core/observer/watcher'
67import { initProxy } from 'core/instance/proxy'
7- import { initState } from 'core/instance/state'
8+ import { initState , getData } from 'core/instance/state'
89import { initRender } from 'core/instance/render'
910import { initEvents } from 'core/instance/events'
1011import { initProvide , initInjections } from 'core/instance/inject'
11- import { initLifecycle , mountComponent , callHook } from 'core/instance/lifecycle'
12+ import { initLifecycle , callHook } from 'core/instance/lifecycle'
1213import { initInternalComponent , resolveConstructorOptions } from 'core/instance/init'
1314import { registerComponentHook , updateComponentData } from '../../util/index'
1415
@@ -55,8 +56,25 @@ function initVirtualComponent (options: Object = {}) {
5556 initProvide ( vm ) // resolve provide after data/props
5657 callHook ( vm , 'created' )
5758
59+ // send initial data to native
60+ const data = vm . $options . data
61+ const params = typeof data === 'function'
62+ ? getData ( data , vm )
63+ : data || { }
64+ if ( isPlainObject ( params ) ) {
65+ updateComponentData ( componentId , params )
66+ }
67+
5868 registerComponentHook ( componentId , 'lifecycle' , 'attach' , ( ) => {
59- mountComponent ( vm )
69+ callHook ( vm , 'beforeMount' )
70+
71+ const updateComponent = ( ) => {
72+ vm . _update ( vm . _vnode , false )
73+ }
74+ new Watcher ( vm , updateComponent , noop , null , true )
75+
76+ vm . _isMounted = true
77+ callHook ( vm , 'mounted' )
6078 } )
6179
6280 registerComponentHook ( componentId , 'lifecycle' , 'detach' , ( ) => {
@@ -65,25 +83,53 @@ function initVirtualComponent (options: Object = {}) {
6583}
6684
6785// override Vue.prototype._update
68- function updateVirtualComponent ( vnode : VNode , hydrating ?: boolean ) {
69- // TODO
70- updateComponentData ( this . $options . componentId , { } )
86+ function updateVirtualComponent ( vnode ?: VNode ) {
87+ const vm : Component = this
88+ const componentId = vm . $options . componentId
89+ if ( vm . _isMounted ) {
90+ callHook ( vm , 'beforeUpdate' )
91+ }
92+ vm . _vnode = vnode
93+ if ( vm . _isMounted && componentId ) {
94+ // TODO: data should be filtered and without bindings
95+ const data = Object . assign ( { } , vm . _data )
96+ updateComponentData ( componentId , data , ( ) => {
97+ callHook ( vm , 'updated' )
98+ } )
99+ }
71100}
72101
73102// listening on native callback
74103export function resolveVirtualComponent ( vnode : MountedComponentVNode ) : VNode {
75104 const BaseCtor = vnode . componentOptions . Ctor
76105 const VirtualComponent = BaseCtor . extend ( { } )
106+ const cid = VirtualComponent . cid
77107 VirtualComponent . prototype . _init = initVirtualComponent
78108 VirtualComponent . prototype . _update = updateVirtualComponent
79109
80110 vnode . componentOptions . Ctor = BaseCtor . extend ( {
81111 beforeCreate ( ) {
82- registerComponentHook ( VirtualComponent . cid , 'lifecycle' , 'create' , componentId => {
112+ // const vm: Component = this
113+
114+ // TODO: listen on all events and dispatch them to the
115+ // corresponding virtual components according to the componentId.
116+ // vm._virtualComponents = {}
117+ const createVirtualComponent = ( componentId , propsData ) => {
83118 // create virtual component
84- const options = { componentId }
85- return new VirtualComponent ( options )
86- } )
119+ // const subVm =
120+ new VirtualComponent ( {
121+ componentId,
122+ propsData
123+ } )
124+ // if (vm._virtualComponents) {
125+ // vm._virtualComponents[componentId] = subVm
126+ // }
127+ }
128+
129+ registerComponentHook ( cid , 'lifecycle' , 'create' , createVirtualComponent )
130+ } ,
131+ beforeDestroy ( ) {
132+ delete this . _virtualComponents
87133 }
88134 } )
89135}
0 commit comments