0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00

Merge pull request #357 from sveltejs/gh-356

fix two-way binding for components inside each-blocks
This commit is contained in:
Rich Harris 2017-03-08 12:25:44 -05:00 committed by GitHub
commit be8efcc9f2
5 changed files with 46 additions and 2 deletions

View File

@ -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}`} });
}
` );
@ -151,7 +153,6 @@ export default function createBinding ( generator, node, attribute, current, loc
updateElement = deindent`
var ${value} = ${contextual ? attribute.value : `root.${attribute.value}`};
console.log( 'value', ${value} );
for ( var ${i} = 0; ${i} < ${local.name}.options.length; ${i} += 1 ) {
var ${option} = ${local.name}.options[${i}];

View File

@ -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}`;

View File

@ -0,0 +1 @@
<span>{{value.id}}</span>

View File

@ -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();
}
};

View File

@ -0,0 +1,13 @@
{{#each a as x}}
<Widget bind:value='x'/>
{{/each}}
<script>
import Widget from './Widget.html';
export default {
components: {
Widget
}
};
</script>