mirror of https://github.com/sveltejs/svelte
30 lines
573 B
30 lines
573 B
<script>
|
|
import Todo from './Todo.svelte';
|
|
|
|
let todos = [
|
|
{ id: 1, done: true, text: 'wash the car' },
|
|
{ id: 2, done: false, text: 'take the dog for a walk' },
|
|
{ id: 3, done: false, text: 'mow the lawn' }
|
|
];
|
|
|
|
function toggle(toggled) {
|
|
todos = todos.map(todo => {
|
|
if (todo === toggled) {
|
|
// return a new object
|
|
return {
|
|
id: todo.id,
|
|
text: todo.text,
|
|
done: !todo.done
|
|
};
|
|
}
|
|
|
|
// return the same object
|
|
return todo;
|
|
});
|
|
}
|
|
</script>
|
|
|
|
<h2>Todos</h2>
|
|
{#each todos as todo}
|
|
<Todo {todo} on:click={() => toggle(todo)}/>
|
|
{/each} |