19 March 2012
All
In Part 1: Getting started of this tutorial, we set up the basic infrastructure for the Wine Cellar application. The application so far is read-only, and thus it only allows you to retrieve a list of wines and display the details of the wine you select.
In this second installment, you will add the ability to create, update, and delete (CRUD) wines.
HTTP Method |
URL |
Action |
GET |
/api/wines |
Retrieve all wines |
GET |
/api/wines/10 |
Retrieve wine with id == 10 |
POST |
/api/wines |
Add a new wine |
PUT |
/api/wines/10 |
Update wine with id == 10 |
DELETE |
/api/wines/10 |
Delete wine with id == 10 |
A PHP version of these services (using the Slim framework) is available as part of the download. A similar Java version of the API (using JAX-RS) is available as part of this post.
If your persistence layer is not available through RESTful services, you can override Backbone.sync. From the documentation:
“Backbone.sync is the function that Backbone calls every time it attempts to read or save a model to the server. By default, it uses (jQuery/Zepto).ajax to make a RESTful JSON request. You can override it in order to use a different persistence strategy, such as WebSockets, XML transport, or Local Storage.”
I do not discuss using non-RESTful services in this tutorial. See the documentation for more information.
You can run the application that you will code in this tutorial. The create, update, delete features are disabled in this online version.
Here is the code for Part 2. I discuss key changes below.
Code highlights
Wine (lines 2-14)
Part 2 adds two attributes to the Wine Model, as follows:
WineListView (lines 22-40)
When user adds a new wine, you want it to automatically appear in the list. To make that happen, bind the View to the add event of the WineListView model (which is the collection of wines). When that event is fired, the application creates and adds a new instance of WineListItemView to the list.
WineListItemView (lines 42-62)
In the spirit of encapsulation, the event handlers for the Save and Delete buttons are defined inside WineView, as opposed to defining them as free-hanging code blocks outside the “class” definitions. Use the Backbone.js Events syntax, which uses the jQuery delegate mechanism behind the scenes.
There are always different approaches to update the model based on user input in a form:
This discussion is not specific to Backbone.js and is therefore beyond the scope of this post. For simplicity, I used the delayed approach here. However I still wired the change event, and use it to log changes to the console. I found this very useful when debugging the application, and particularly to make sure I had cleaned up my bindings (see the close function). If you see the change event firing multiple times, you probably didn’t clean up your bindings appropriately.
Backbone.js Views are typically used to render domain models (as done in WineListView, WineListItemView, and Wine View). But they can also be used to create composite UI components. For example, in this application, we define a Header View (a toolbar) that could be made of different components and which encapsulates its own logic.
The application so far doesn’t support deep-linking. For example, it doesn’t include the functionality to select a wine in the list, grab the URL in the address bar and paste it in another browser window: these features don’t yet work. In Part 3: Deep linking and application states, you will add complete support for deep linking.
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.