mirror of https://github.com/sveltejs/svelte
Merge pull request #694 from sveltejs/oncreate-before-bindings
Call `oncreate` functions before updating bindingspull/701/head
commit
459ca953f4
@ -0,0 +1,15 @@
|
|||||||
|
<Two bind:foo/>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import Two from './Two.html';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
components: {
|
||||||
|
Two
|
||||||
|
},
|
||||||
|
|
||||||
|
oncreate() {
|
||||||
|
this.snapshot = this.get('foo');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</script>
|
@ -0,0 +1,15 @@
|
|||||||
|
<script>
|
||||||
|
export default {
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
bar: 1
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
computed: {
|
||||||
|
foo(bar) {
|
||||||
|
return bar * 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</script>
|
@ -0,0 +1,5 @@
|
|||||||
|
export default {
|
||||||
|
test(assert, component) {
|
||||||
|
assert.equal(component.refs.one.snapshot, 2);
|
||||||
|
}
|
||||||
|
};
|
@ -0,0 +1,9 @@
|
|||||||
|
<One ref:one/>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import One from './One.html';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
components: { One }
|
||||||
|
};
|
||||||
|
</script>
|
@ -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>
|
@ -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);
|
||||||
|
}
|
||||||
|
};
|
@ -0,0 +1,3 @@
|
|||||||
|
export default {
|
||||||
|
count: 0
|
||||||
|
};
|
@ -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