06: Add New Todo

Implement to add new todo with flux syntax.

  1. Remove sample todo items from x-state.
  2. Add addTodo invocation to controller element by x-invocations directive.
  3. Set flux code to <input> of new-todo by x-on directive.
  4. Add callback event listener to <input> of new-todo by x-callbacks directive. It works after addTodo invocation.

<body>
  <section class="todoapp"
    x-state="{
      todos: [
        {
          title: 'Todo Title (Active)',
          completed: false
        },
        {
          title: 'Todo Title (Completed)',
          completed: true
        },
      ],
    }"
    x-invocations="{
      addTodo (_, payload, meta) {
        const newTodo = payload.newTodo.trim()
        if (!newTodo) {
          return
        }

        this.todos.push({
          title: newTodo,
          completed: false
        })
      },
    }"
    x-controller
  _>
    <header class="header">
      <h1>todos</h1>
      <input class="new-todo"
        autofocus
        autocomplete="off"
        placeholder="What needs to be done?"

        x-on:change="addTodo({newTodo: launcher$.value})"
        x-callbacks="{
          change (_) {
            _.$.value = ''
          }
        }"
      >
    </header>


  </section>
</body>
</html>