Skip to content

Commit

Permalink
Merge pull request #727 from sveltejs/gh-706
Browse files Browse the repository at this point in the history
clear refs in destroy, not unmount, so that refs are populated in ondestroy
  • Loading branch information
Rich-Harris authored Jul 29, 2017
2 parents 5c4905a + 4d36908 commit dbf635b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/generators/dom/visitors/Element/Ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function visitRef(
`#component.refs.${name} = ${state.parentNode};`
);

block.builders.unmount.addLine(deindent`
block.builders.destroy.addLine(deindent`
if ( #component.refs.${name} === ${state.parentNode} ) #component.refs.${name} = null;
`);

Expand Down
9 changes: 9 additions & 0 deletions test/runtime/samples/ondestroy-before-cleanup/Top.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div ref:element></div>

<script>
export default {
ondestroy() {
this.refOnDestroy = this.refs.element;
}
};
</script>
9 changes: 9 additions & 0 deletions test/runtime/samples/ondestroy-before-cleanup/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
test(assert, component, target) {
const top = component.refs.top;
const div = target.querySelector('div');

component.set({ visible: false });
assert.equal(top.refOnDestroy, div);
}
};
18 changes: 18 additions & 0 deletions test/runtime/samples/ondestroy-before-cleanup/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{{#if visible}}
<Top ref:top></Top>
{{/if}}

<script>
import Top from './Top.html';

export default {
data() {
return {
visible: true
};
},
components: {
Top
}
};
</script>

0 comments on commit dbf635b

Please sign in to comment.