mirror of https://github.com/sveltejs/svelte
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
577 B
31 lines
577 B
6 years ago
|
<script>
|
||
6 years ago
|
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' }
|
||
|
];
|
||
6 years ago
|
|
||
6 years ago
|
function toggle(toggled) {
|
||
1 year ago
|
todos = todos.map((todo) => {
|
||
6 years ago
|
if (todo === toggled) {
|
||
|
// return a new object
|
||
|
return {
|
||
|
id: todo.id,
|
||
|
text: todo.text,
|
||
|
done: !todo.done
|
||
|
};
|
||
|
}
|
||
|
|
||
|
// return the same object
|
||
|
return todo;
|
||
|
});
|
||
6 years ago
|
}
|
||
6 years ago
|
</script>
|
||
6 years ago
|
|
||
6 years ago
|
<h2>Todos</h2>
|
||
|
{#each todos as todo}
|
||
1 year ago
|
<Todo {todo} on:click={() => toggle(todo)} />
|
||
|
{/each}
|