Skip to content

Commit

Permalink
fix two-way binding for components inside each-blocks - closes #356
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Mar 8, 2017
1 parent df70e7b commit 92925ed
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/generators/dom/visitors/attributes/binding/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ export default function createBinding ( generator, node, attribute, current, loc
});
` );

const dependencies = parts[0] in current.contexts ? current.contextDependencies[ parts[0] ] : [ parts[0] ];

local.update.addBlock( deindent`
if ( !${local.name}_updating && '${parts[0]}' in changed ) {
if ( !${local.name}_updating && ${dependencies.map( dependency => `'${dependency}' in changed` ).join( '||' )} ) {
${local.name}._set({ ${attribute.name}: ${contextual ? attribute.value : `root.${attribute.value}`} });
}
` );
Expand Down
5 changes: 5 additions & 0 deletions src/generators/server-side-rendering/visitors/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export default {

return `${attribute.name}: ${value}`;
})
.concat( bindings.map( binding => {
const parts = binding.value.split( '.' );
const value = parts[0] in generator.current.contexts ? binding.value : `root.${binding.value}`;
return `${binding.name}: ${value}`;
}))
.join( ', ' );

const expression = node.name === ':Self' ? generator.name : `template.components.${node.name}`;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<span>{{value.id}}</span>
24 changes: 24 additions & 0 deletions test/generator/samples/component-binding-each-object/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export default {
data: {
a: [{ id: 'foo' }, { id: 'bar' }, { id: 'baz' }]
},

html: `
<span>foo</span><span>bar</span><span>baz</span>
`,

test ( assert, component, target ) {
component.set({
a: [
{ id: 'yep' },
{ id: 'nope' }
]
});

assert.htmlEqual( target.innerHTML, `
<span>yep</span><span>nope</span>
` );

component.destroy();
}
};
13 changes: 13 additions & 0 deletions test/generator/samples/component-binding-each-object/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{#each a as x}}
<Widget bind:value='x'/>
{{/each}}

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

export default {
components: {
Widget
}
};
</script>

0 comments on commit 92925ed

Please sign in to comment.