mirror of
https://github.com/sveltejs/svelte.git
synced 2024-12-01 17:30:59 +01:00
only fire onrender hooks once component is in the DOM – fixes #18
This commit is contained in:
parent
8b365084e6
commit
0f464d79b2
@ -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 || {}`;
|
||||
|
@ -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
|
||||
});
|
||||
` );
|
||||
}
|
||||
|
9
test/compiler/onrender-fires-when-ready/Widget.html
Normal file
9
test/compiler/onrender-fires-when-ready/Widget.html
Normal file
@ -0,0 +1,9 @@
|
||||
<p ref:x>{{inDocument}}</p>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
onrender () {
|
||||
this.set({ inDocument: document.contains( this.refs.x ) })
|
||||
}
|
||||
};
|
||||
</script>
|
10
test/compiler/onrender-fires-when-ready/_config.js
Normal file
10
test/compiler/onrender-fires-when-ready/_config.js
Normal file
@ -0,0 +1,10 @@
|
||||
export default {
|
||||
html: `<div><p>true</p></div>`,
|
||||
|
||||
test ( assert, component, target ) {
|
||||
component.set({ foo: true });
|
||||
assert.htmlEqual( target.innerHTML, `<div><p>true</p>\n<p>true</p></div>` );
|
||||
|
||||
component.teardown();
|
||||
}
|
||||
};
|
17
test/compiler/onrender-fires-when-ready/main.html
Normal file
17
test/compiler/onrender-fires-when-ready/main.html
Normal file
@ -0,0 +1,17 @@
|
||||
<div>
|
||||
<Widget/>
|
||||
|
||||
{{#if foo}}
|
||||
<Widget/>
|
||||
{{/if}}
|
||||
</div>
|
||||
|
||||
<script>
|
||||
import Widget from './Widget.html';
|
||||
|
||||
export default {
|
||||
components: {
|
||||
Widget
|
||||
}
|
||||
};
|
||||
</script>
|
Loading…
Reference in New Issue
Block a user