In tutorial we are going to continue learning about BackboneJS: CRUD.
BackboneJS Tutorial series:
- Backbone.js for Absolute Beginners - Getting started (Part 1: Intro)
- Backbone.js for absolute beginners - getting started (part 2: Models, Collections and Views)
- Backbone.js for absolute beginners - getting started (part 3: CRUD) 👈 you are here
- Backbone.js for absolute beginners - getting started (part 4: Routers)
Todo item list CRUD
There are a couple of features that we could improve. Let’s implement the CRUD (Create-Read-Update-Delete) for the item list.
C-reate
We are already can create item list from the console (2.3) and also from the UI (2.4.3). So, it’s done.
U-pdate
What if you make a mistake and want to change the text on some of your to-do list. Furthermore, you can notice that the checkboxes states are not persistent when you reload the pages. Let’s fix both problems.
1.- You want to respond to a double click event showing up a text box, where the user can change the text. First, let’s add the HTML in the item-template
template below the label tag.
<input class="edit" value="<%- title %>">
2.- If you refresh, you will notice that there are both displaying at the same time. So, you can hide them properly with the following CSS.
1 |
|
3.- Then, we need to add the events to the TodoView class to respond to the changes.
1 |
|
You can find the diff that were added to implement the update feature.
Here are the changes to fix the update for the checkboxes.
D-elete
To be able to remove to-do items, we need to add a remove button in each item and listen to the click event on it, which will trigger the destroy function in the selected todo object.
1.- Add the HTML markup for the remove button.
1 |
|
2.- Listen for the click event in the button that you just created.
1 |
|
3.- Add the destroy method to the TodoView.
1 |
|
You can download the full working code so far in here and you can visualize the changes needed to implement the delete feature in here
What’s next?
Continue with the 4th part and learn about Backbone’s Routes!