Coding > Web Development > Backbone
Last updated September 13th 2012 96.3k 0
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)
There are a couple of features that we could improve. Let’s implement the CRUD (Create-Read-Update-Delete) for the item list.
We are already can create item list from the console (2.3) and also from the UI (2.4.3). So, it’s done.
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.
CSSFull Code
|
|
3.- Then, we need to add the events to the TodoView class to respond to the changes.
Todo ModelFull Code
|
|
You can find the diff that were added to implement the update feature.
Here are the changes to fix the update for the checkboxes.
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.
Remove Button into item templateFull Code
|
|
2.- Listen for the click event in the button that you just created.
Add event listeners for the Remove Button in app.TodoViewFull Code
|
|
3.- Add the destroy method to the TodoView.
Add the destroy method to app.TodoViewFull Code
|
|
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
Continue with the 4th part and learn about Backbone’s Routes!
Thanks for reading this far. Here are some things you can do next:
- Found a typo? Edit this post.
- Got questions? comment below.
- Was it useful? Show your support and share it.
Backbone.js for absolute beginners - getting started (part 4: Routers)
Backbone.js for absolute beginners - getting started (part 2: Models, Collections and Views)
Subscribe & stay up to date!