Skip to content

Commit

Permalink
each binding with store props
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed Aug 19, 2020
1 parent cd95654 commit f04bcef
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
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>

0 comments on commit f04bcef

Please sign in to comment.