mirror of
https://github.com/sveltejs/svelte.git
synced 2024-12-01 17:30:59 +01:00
call oncreate functions before bindings
This commit is contained in:
parent
ef33466c12
commit
d707f6a3b2
@ -122,10 +122,10 @@ export default function dom(
|
||||
@dispatchObservers( this, this._observers.pre, newState, oldState );
|
||||
${block.hasUpdateMethod && `this._fragment.update( newState, this._state );`}
|
||||
@dispatchObservers( this, this._observers.post, newState, oldState );
|
||||
${generator.hasComplexBindings &&
|
||||
`while ( this._bindings.length ) this._bindings.pop()();`}
|
||||
${(generator.hasComponents || generator.hasIntroTransitions) &&
|
||||
`this._flush();`}
|
||||
${generator.hasComplexBindings &&
|
||||
`while ( this._bindings.length ) this._bindings.pop()();`}
|
||||
`;
|
||||
|
||||
if (hasJs) {
|
||||
@ -219,10 +219,10 @@ export default function dom(
|
||||
this._fragment.${block.hasIntroMethod ? 'intro' : 'mount'}( options.target, null );
|
||||
}
|
||||
|
||||
${generator.hasComplexBindings &&
|
||||
`while ( this._bindings.length ) this._bindings.pop()();`}
|
||||
${(generator.hasComponents || generator.hasIntroTransitions) &&
|
||||
`this._flush();`}
|
||||
${generator.hasComplexBindings &&
|
||||
`while ( this._bindings.length ) this._bindings.pop()();`}
|
||||
|
||||
${templateProperties.oncreate &&
|
||||
deindent`
|
||||
|
@ -191,6 +191,7 @@ function SvelteComponent ( options ) {
|
||||
this._fragment.create();
|
||||
this._fragment.mount( options.target, null );
|
||||
}
|
||||
|
||||
this._flush();
|
||||
}
|
||||
|
||||
|
@ -70,6 +70,7 @@ function SvelteComponent ( options ) {
|
||||
this._fragment.create();
|
||||
this._fragment.mount( options.target, null );
|
||||
}
|
||||
|
||||
this._flush();
|
||||
}
|
||||
|
||||
|
30
test/runtime/samples/oncreate-before-bindings/Nested.html
Normal file
30
test/runtime/samples/oncreate-before-bindings/Nested.html
Normal file
@ -0,0 +1,30 @@
|
||||
{{#each things as thing}}
|
||||
<Visibility bind:isVisible="visibilityMap[thing]">
|
||||
<p>{{thing}} ({{visibilityMap[thing]}})</p>
|
||||
</Visibility>
|
||||
{{/each}}
|
||||
|
||||
<script>
|
||||
import Visibility from './Visibility.html';
|
||||
import counter from './counter.js';
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
things: ['first thing', 'second thing'],
|
||||
visibilityMap: {}
|
||||
};
|
||||
},
|
||||
|
||||
computed: {
|
||||
visibleThings: (things, visibilityMap) => {
|
||||
counter.count += 1;
|
||||
return things.filter(text => visibilityMap[text]);
|
||||
}
|
||||
},
|
||||
|
||||
components: {
|
||||
Visibility
|
||||
}
|
||||
};
|
||||
</script>
|
@ -0,0 +1,11 @@
|
||||
<div>
|
||||
{{yield}}
|
||||
</div>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
oncreate() {
|
||||
this.set({ isVisible: true });
|
||||
}
|
||||
};
|
||||
</script>
|
21
test/runtime/samples/oncreate-before-bindings/_config.js
Normal file
21
test/runtime/samples/oncreate-before-bindings/_config.js
Normal file
@ -0,0 +1,21 @@
|
||||
import counter from './counter.js';
|
||||
|
||||
export default {
|
||||
'skip-ssr': true,
|
||||
|
||||
html: `
|
||||
<div><p>first thing (true)</p></div>
|
||||
<div><p>second thing (true)</p></div>
|
||||
`,
|
||||
|
||||
test(assert, component) {
|
||||
const visibleThings = component.get('visibleThings');
|
||||
assert.deepEqual(visibleThings, ['first thing', 'second thing']);
|
||||
|
||||
const snapshots = component.snapshots;
|
||||
assert.deepEqual(snapshots, [visibleThings]);
|
||||
|
||||
// TODO minimise the number of recomputations during oncreate
|
||||
// assert.equal(counter.count, 1);
|
||||
}
|
||||
};
|
3
test/runtime/samples/oncreate-before-bindings/counter.js
Normal file
3
test/runtime/samples/oncreate-before-bindings/counter.js
Normal file
@ -0,0 +1,3 @@
|
||||
export default {
|
||||
count: 0
|
||||
};
|
24
test/runtime/samples/oncreate-before-bindings/main.html
Normal file
24
test/runtime/samples/oncreate-before-bindings/main.html
Normal file
@ -0,0 +1,24 @@
|
||||
<Nested bind:visibleThings="visibleThings" />
|
||||
|
||||
<script>
|
||||
import Nested from './Nested.html';
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
visibleThings: []
|
||||
};
|
||||
},
|
||||
|
||||
components: {
|
||||
Nested
|
||||
},
|
||||
|
||||
oncreate() {
|
||||
this.snapshots = [];
|
||||
this.observe('visibleThings', things => {
|
||||
this.snapshots.push(things);
|
||||
});
|
||||
}
|
||||
};
|
||||
</script>
|
Loading…
Reference in New Issue
Block a user