Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / HTML5, CSS3, and JavaScript /

Backbone.js Wine Cellar tutorial – Part 3: Deep linking and application states

by Christophe Coenraets

Christophe Coenraets
  • http://coenraets.org/blog/

Content

  • Problem 2: Updating the URL after a user adds a wine
  • Problem 3: Updating the URL while creating a new wine
  • Where to go from here

Created

19 March 2012

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
HTML5 JavaScript

Requirements

Prerequisite knowledge

Basic understanding of JavaScript. If you haven’t already read Part 1: Getting started and Part 2: CRUD, do so before proceeding with this article.

Sample files:

  • Download the source code for Part 3 of the Wine Cellar application on GitHub.
  • You will need the RESTful services to run this application. A PHP version (using the Slim framework) is available as part of the download.
  • You can download different versions of the Wine Cellar application:
    • Download a version of this application with a Java back-end (using JAX-RS and Jersey) on GitHub. Read about the Java version of this application in my blog post, Using Backbone.js with a RESTful Java Back-End.
    • Download the non-Backbone Java back-end or the PHP back-end.

User level

All

In Part 1: Getting started, we set up the basic infrastructure for the Wine Cellar application. In Part 2: CRUD, we added the ability to create, update, and delete (CRUD) wines.

There are a few remaining issues in the application. They are all related to “deep linking.” The application needs the capability to continuously keep its URL in sync with its current state. This allows you and your users to grab the URL from the address bar at any point in time during the course of the application and re-use or share it to go back to the exact same state.

In Part 3, we add support for deep linking in the Wine Cellar application.

Run the application for Part 3.  The create/update/delete features are disabled in this online version.

The problem:

  1. Select a specific wine from the list in the application. The URL looks appears as follows: http://www.coenraets.org/backbone-cellar/part2/#wines/[id].
  2. Now, do one of the following options within the running application:
    • Grab that URL from the address bar and try to access it from another browser window or tab
    • Simply click your browser’s Refresh button

You will see get an empty screen with no data. If you look at a debug console (for example, in Chrome’s Developer Tools), you’ll see the following message:

167 Uncaught TypeError: Cannot call method 'get' of undefined

Let’s take a look at the code, as follows:

Figure 1. The problem causing a blank screen when copying the URL to another browser or tabbed browser window
Figure 1. The problem causing a blank screen when copying the URL to another browser or tabbed browser window

The problem is on line 20. We wrote the code assuming that a wine collection (this.wineList) already exists, and we are trying to “get” a specific item from that list. That works well when a user starts the application with the default (“”) route. But this.wineList won’t exist if a user starts the application with the “wines/:id” route. There are different ways to address the issue, as described in the following options.  

We could modify the wineDetails function to fetch the requested item directly.

Figure 2. One option is to modify the wineDetails function to fetch the requested item directly
Figure 2. One option is to modify the wineDetails function to fetch the requested item directly

That takes care of loading the wine details in the form, but the wine list remains empty when a user starts the application with “wines/:id” route.

We could add the following line of code to load the list if it doesn’t exist.

Figure 3. Another option is to load the list if it doesn’t exist
Figure 3. Another option is to load the list if it doesn’t exist

But now the wine model that is part of the collection and the wine model fetched separately are two different objects, which means that data binding and View synchronization will not work as expected.

We could check whether the collection exists in the wineDetails function. If it does, we simply “get” the requested item and render it as we did before. If it doesn’t, we store the requested id in a variable, and then invoke the existing list() function to populate the list. We then modify the list() function. When we get the list from the server (on success), we check if there was a requested id. If there was, we invoke the wineDetails function to render the corresponding item.

Figure 4. Checking whether the collection exists before deciding the next action. This last option is the preferable one.
Figure 4. Checking whether the collection exists before deciding the next action. This last option is the preferable one.

Problem 2: Updating the URL after a user adds a wine

Run the application for Part 3.  The create/update/delete features are disabled in this online version.

The problem:

  1. Add a new Wine.
  2. Click Save. The id that has been assigned to the newly created wine appears in the form field. However the URL is still:
http://localhost/backbone-cellar/part2/ when it should really be: http://localhost/backbone-cellar/part2/#wines/[id].

You can easily fix this issue by using the router’s navigate function to change the URL. The second argument (false), indicates that we actually don’t want to “execute” that route, we simply want to change the URL.

Figure 5. Fixing the problem of updating the URL after a user adds a wine
Figure 5. Fixing the problem of updating the URL after a user adds a wine

Problem 3: Updating the URL while creating a new wine

Run the application for Part 3.  The create/update/delete features are disabled in this online version.

The problem:

  1. Select a wine in the list.
  2. The URL looks like this: http://localhost/backbone-cellar/part2/#wines/[id]
  3. Click the “Add Wine” button. You will see an empty form to enter a new wine, but notice that the URL is still unchanged (http://localhost/backbone-cellar/part2/#wines/[id]). In other words, it doesn’t reflect the current state of the application.

To fix the problem in this case, add a new “route”:

Figure 6. Fixing the problem by adding a new “route”
Figure 6. Fixing the problem by adding a new “route”

The router’s newWine method is as follows:

Figure 7. Next, rewrite the newWine method
Figure 7. Next, rewrite the newWine method

Change the HeaderView newWine() method as follows:

Figure 8. Changing the HeaderView newWine() method
Figure 8. Changing the HeaderView newWine() method

Where to go from here

Download the final version of the code on GitHub. Additionally, I posted a “Postface” to this series with some lessons learned and an improved version of the app. Make sure you read about it in my blog post: Backbone.js: Lessons learned and improved sample application.

Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License+Adobe Commercial Rights

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.

More Like This

  • Using the Geolocation API
  • CSS3 basics
  • Developing HTML5 games with Impact JavaScript game engine and Dreamweaver CS5.5
  • Introducing the HTML5 storage APIs
  • Introducing theexpressiveweb.com beta
  • Adobe, standards, and HTML5
  • Real-time data exchange in HTML5 with WebSockets
  • Object types in JavaScript
  • Pseudo-classical object-oriented programming in JavaScript with Minion
  • Backbone.js Wine Cellar tutorial – Part 2: CRUD

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement