Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: bind:this in each with objects\array #4759

Merged
merged 3 commits into from
May 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/compiler/compile/render_dom/wrappers/shared/bind_this.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export default function bind_this(component: Component, block: Block, binding: B
const args = [];
for (const id of contextual_dependencies) {
args.push(id);
if (block.variables.has(id.name)) {
if (block.renderer.context_lookup.get(id.name).is_contextual) continue;
}
block.add_variable(id, block.renderer.reference(id.name));
}

Expand Down
14 changes: 14 additions & 0 deletions test/runtime/samples/binding-this-each-object-props/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default {
html: ``,

async test({ assert, component, target }) {
component.visible = true;
assert.htmlEqual(target.innerHTML, `
<div>b</div><div>b</div><div>c</div><div>c</div>
`);
assert.equal(component.items1[1], target.querySelector('div'));
assert.equal(component.items2[1], target.querySelector('div:nth-child(2)'));
assert.equal(component.items1[2], target.querySelector('div:nth-child(3)'));
assert.equal(component.items2[2], target.querySelector('div:last-child'));
}
};
13 changes: 13 additions & 0 deletions test/runtime/samples/binding-this-each-object-props/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
export const items1 = {};
export const items2 = {};
export let data = [
{id: 1, text: "b"},
{id: 2, text: "c"},
];
</script>

{#each data as item (item.id)}
<div bind:this={items1[item.id]}>{item.text}</div>
<div bind:this={items2[item.id]}>{item.text}</div>
{/each}
14 changes: 14 additions & 0 deletions test/runtime/samples/binding-this-each-object-spread/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default {
html: ``,

async test({ assert, component, target }) {
component.visible = true;
assert.htmlEqual(target.innerHTML, `
<div>a</div><div>a</div><div>b</div><div>b</div>
`);
assert.equal(component.items1[1], target.querySelector('div'));
assert.equal(component.items2[1], target.querySelector('div:nth-child(2)'));
assert.equal(component.items1[2], target.querySelector('div:nth-child(3)'));
assert.equal(component.items2[2], target.querySelector('div:last-child'));
}
};
13 changes: 13 additions & 0 deletions test/runtime/samples/binding-this-each-object-spread/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
export const items1 = {};
export const items2 = {};
var data = [
{id: 1, text: "a"},
{id: 2, text: "b"},
];
</script>

{#each data as {id, text} (id)}
<div bind:this={items1[id]}>{text}</div>
<div bind:this={items2[id]}>{text}</div>
{/each}