diff --git a/compiler/generate/index.js b/compiler/generate/index.js index f950e7b393..a46cc432c8 100644 --- a/compiler/generate/index.js +++ b/compiler/generate/index.js @@ -299,6 +299,12 @@ export default function generate ( parsed, source, options ) { initStatements.push( `if ( !addedCss ) addCss();` ); } + if ( generator.hasComponents ) { + initStatements.push( deindent` + this.__renderHooks = []; + ` ); + } + if ( generator.hasComplexBindings ) { initStatements.push( deindent` this.__bindings = []; @@ -311,8 +317,26 @@ export default function generate ( parsed, source, options ) { initStatements.push( `var mainFragment = renderMainFragment( state, this, options.target );` ); } + if ( generator.hasComponents ) { + const statement = deindent` + while ( this.__renderHooks.length ) { + var hook = this.__renderHooks.pop(); + hook.fn.call( hook.context ); + } + `; + + initStatements.push( statement ); + setStatements.push( statement ); + } + if ( templateProperties.onrender ) { - initStatements.push( `template.onrender.call( this );` ); + initStatements.push( deindent` + if ( options.parent ) { + options.parent.__renderHooks.push({ fn: template.onrender, context: this }); + } else { + template.onrender.call( this ); + } + ` ); } const initialState = templateProperties.data ? `Object.assign( template.data(), options.data )` : `options.data || {}`; diff --git a/compiler/generate/visitors/Element.js b/compiler/generate/visitors/Element.js index e545128654..0b6f9e7b8d 100644 --- a/compiler/generate/visitors/Element.js +++ b/compiler/generate/visitors/Element.js @@ -20,6 +20,8 @@ export default { }; if ( isComponent ) { + generator.hasComponents = true; + addComponentAttributes( generator, node, local ); if ( local.staticAttributes.length || local.dynamicAttributes.length || local.bindings.length ) { @@ -54,13 +56,15 @@ export default { var ${name} = new template.components.${node.name}({ target: ${generator.current.target}, + parent: component, data: ${name}_initialData }); ` ); } else { local.init.unshift( deindent` var ${name} = new template.components.${node.name}({ - target: ${generator.current.target} + target: ${generator.current.target}, + parent: component }); ` ); } diff --git a/test/compiler/onrender-fires-when-ready/Widget.html b/test/compiler/onrender-fires-when-ready/Widget.html new file mode 100644 index 0000000000..10de81c6d4 --- /dev/null +++ b/test/compiler/onrender-fires-when-ready/Widget.html @@ -0,0 +1,9 @@ +
{{inDocument}}
+ + diff --git a/test/compiler/onrender-fires-when-ready/_config.js b/test/compiler/onrender-fires-when-ready/_config.js new file mode 100644 index 0000000000..14db20be9e --- /dev/null +++ b/test/compiler/onrender-fires-when-ready/_config.js @@ -0,0 +1,10 @@ +export default { + html: `true
true
\ntrue