Creating New ToDo Items

Welcome back! Now that you’ve set up a basic ToDo list, it's time to add functionality that allows users to create new items. This step enables direct interaction with the application by adding new tasks to the ToDo list.

By the end of this lesson, you'll have a form for users to input and create new ToDo items, with server-side handling to store them.

What This Lesson Covers

In this lesson, you'll learn how to:

  • Extend your TodoService to allow the creation of new ToDo items.
  • Update your TodoController to handle form submissions.

These enhancements will make your ToDo app interactive, allowing users to add new items directly through the interface.

Enhancing the Todo Service

In the TodoService, we'll create a create method that:

  • Accepts a title, a dueDate (required), and an optional description.
  • Creates a new Todo item with a unique ID using Date.now().
  • Adds the new item to the existing list of ToDo items and returns a success message.

Here's how this method is implemented:

The create method in the TodoService adds the new item to the todos array and returns a success message.

Updating the Todo Controller

Next, let's update the TodoController to handle form submissions. This will allow the app to process data entered by users and add new items to the ToDo list.

The create method in the controller will:

  • Listen for POST requests when a user submits a form.
  • Extract the title, dueDate, and description from the form using the @Body decorator.
  • Call the create method from the TodoService to add the new item to the list.
  • Use the @Render('index') decorator to re-render the updated list with a success message.

Here's how to implement this:

In this controller:

  • The @Post() method listens for form submissions.
  • The @Body decorator extracts the form data, passing it to the create method in TodoService.
  • After creating the new item, it returns the updated list of ToDo items along with a success message using the @Render('index') decorator.
Modifying the View with a Form

Finally, let's add a basic form to the view that allows users to create new ToDo items. This form will send a POST request to the controller when submitted.

Here's how to include the form in index.hbs:

In this view:

  • The <form> sends a POST request to /todos when the user submits it.
  • The form contains fields for title, dueDate, and an optional description.
  • The "Add ToDo" button submits the form, triggering the create method in the TodoController.

When the form is submitted, the TodoController processes the data, adds the new item to the list, and displays a success message along with the updated list.

Why This Matters

Adding a "Create" functionality is crucial for building dynamic, interactive applications. It allows users to interact with the app and modify its content directly. With this implementation, your ToDo app becomes a more engaging and practical tool.

By implementing this feature, you've learned how to handle form submissions, process data on the server side, and update the UI to reflect changes. Ready to try it out? Run the code and create some ToDo items!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal