0
0
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:
Rich Harris 2017-07-06 08:51:04 -04:00
parent ef33466c12
commit d707f6a3b2
8 changed files with 95 additions and 4 deletions

View File

@ -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`

View File

@ -191,6 +191,7 @@ function SvelteComponent ( options ) {
this._fragment.create();
this._fragment.mount( options.target, null );
}
this._flush();
}

View File

@ -70,6 +70,7 @@ function SvelteComponent ( options ) {
this._fragment.create();
this._fragment.mount( options.target, null );
}
this._flush();
}

View 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>

View File

@ -0,0 +1,11 @@
<div>
{{yield}}
</div>
<script>
export default {
oncreate() {
this.set({ isVisible: true });
}
};
</script>

View 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);
}
};

View File

@ -0,0 +1,3 @@
export default {
count: 0
};

View 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>