Skip to content

Instantly share code, notes, and snippets.

@Rich-Harris
Last active February 22, 2017 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rich-Harris/115f8c1ae72c3e8b4eafedc0937439ff to your computer and use it in GitHub Desktop.
Save Rich-Harris/115f8c1ae72c3e8b4eafedc0937439ff to your computer and use it in GitHub Desktop.
Two-way binding inside {{#each}} blocks
bundle.js
node_modules
.vscode
.DS_Store
cleanup.sh
<div class='component'>
<input bind:value>
</div>
<html>
<body>
<main></main>
<script src='bundle.js'></script>
</body>
</html>
<section>
<h2>direct</h2>
{{#each a as x}}
<!--TODO this doesn't work with components!-->
<!--<Component bind:value='prop'/>-->
<input bind:value='x'>
{{/each}}
<p>{{a.join(', ')}}</p>
</section>
<section>
<h2>nested</h2>
{{#each b as x}}
<!--TODO this doesn't work with components!-->
<!--<Component bind:value='prop'/>-->
<input bind:value='x.name'>
{{/each}}
<p>{{b.map(getName).join(', ')}}</p>
</section>
<script>
import Component from './Component.html';
export default {
data: function () {
return {
a: [ 'foo', 'bar', 'baz' ],
b: [{ name: 'foo' }, { name: 'bar' }, { name: 'baz' }],
getName: function ( x ) {
return x.name;
}
}
},
components: {
Component
}
};
</script>
import Main from './Main.html';
new Main({
target: document.querySelector( 'main' )
});
{
"devDependencies": {
"rollup": "^0.41.4",
"rollup-plugin-svelte": "^1.6.0"
},
"scripts": {
"build": "rollup -c"
}
}
import svelte from 'rollup-plugin-svelte';
export default {
entry: 'main.js',
dest: 'bundle.js',
format: 'iife',
plugins: [ svelte() ]
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment