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

each binding with store props #5289

Merged
merged 3 commits into from
Aug 27, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Fix using `<Namespaced.Component/>`s in child `{#await}`/`{#each}` contexts ([#5255](https://github.com/sveltejs/svelte/issues/5255))
* Fix using `<svelte:component>` in `{:catch}` ([#5259](https://github.com/sveltejs/svelte/issues/5259))
* Fix setting one-way bound `<input>` `value` to `undefined` when it has spread attributes ([#5270](https://github.com/sveltejs/svelte/issues/5270))
* Fix deep two-way bindings inside an `{#each}` involving a store ([#5286](https://github.com/sveltejs/svelte/issues/5286))

## 3.24.1

Expand Down
27 changes: 11 additions & 16 deletions src/compiler/compile/render_dom/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ import { b, x } from 'code-red';
import { Node, Identifier, ArrayPattern } from 'estree';
import { is_head } from './wrappers/shared/is_head';

export interface Bindings {
object: Identifier;
property: Identifier;
snippet: Node;
store: string;
tail: Node;
modifier: (node: Node) => Node;
}

export interface BlockOptions {
parent?: Block;
name: Identifier;
type: string;
renderer?: Renderer;
comment?: string;
key?: Identifier;
bindings?: Map<string, {
object: Identifier;
property: Identifier;
snippet: Node;
store: string;
tail: Node;
modifier: (node: Node) => Node;
}>;
bindings?: Map<string, Bindings>;
dependencies?: Set<string>;
}

Expand All @@ -36,14 +38,7 @@ export default class Block {

dependencies: Set<string> = new Set();

bindings: Map<string, {
object: Identifier;
property: Identifier;
snippet: Node;
store: string;
tail: Node;
modifier: (node: Node) => Node;
}>;
bindings: Map<string, Bindings>;

chunks: {
declarations: Array<Node | Node[]>;
Expand Down
8 changes: 3 additions & 5 deletions src/compiler/compile/render_dom/wrappers/EachBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import FragmentWrapper from './Fragment';
import { b, x } from 'code-red';
import ElseBlock from '../../nodes/ElseBlock';
import { Identifier, Node } from 'estree';
import get_object from '../../utils/get_object';

export class ElseBlockWrapper extends Wrapper {
node: ElseBlock;
Expand Down Expand Up @@ -139,11 +140,8 @@ export default class EachBlockWrapper extends Wrapper {
view_length: fixed_length === null ? x`${iterations}.length` : fixed_length
};

const store =
node.expression.node.type === 'Identifier' &&
node.expression.node.name[0] === '$'
? node.expression.node.name.slice(1)
: null;
const object = get_object(node.expression.node);
const store = object.type === 'Identifier' && object.name[0] === '$' ? object.name.slice(1) : null;

node.contexts.forEach(prop => {
this.block.bindings.set(prop.key.name, {
Expand Down
14 changes: 14 additions & 0 deletions test/runtime/samples/store-each-binding-deep/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default {
async test({ assert, target, window }) {
const input = target.querySelector('input');

const event = new window.Event('input');
input.value = 'changed';
await input.dispatchEvent(event);

assert.htmlEqual(target.innerHTML, `
<input>
<p>changed</p>
`);
}
};
11 changes: 11 additions & 0 deletions test/runtime/samples/store-each-binding-deep/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
import { writable } from 'svelte/store';

let itemStore = writable({prop: {things: [{name: "item store"}]}});
</script>

{#each $itemStore.prop.things as thing }
<input bind:value={thing.name} >
{/each}

<p>{$itemStore.prop.things[0].name}</p>