Adobe
Products

Top destinations

  • Adobe Creative Cloud
  • Creative Suite
  • Adobe Marketing Cloud
  • Acrobat
  • Photoshop
  • SiteCatalyst
  • Students
  • Elements family

Adobe Creative Cloud

  • What is Adobe Creative Cloud?
  • Design
  • Web
  • Photography
  • Video
  • Students
  • Teams
  • Enterprise
  • Educational institutions

Design and photography

  • Photoshop
  • Illustrator
  • InDesign
  • Adobe Muse
  • Lightroom

Video

  • Adobe Premiere
  • After Effects

Web development and HTML5

  • Edge Tools & Services [opens in a new window]
  • Dreamweaver
  • Gaming [opens in a new window]

Adobe Marketing Cloud

  • What is Adobe Marketing Cloud?
  • Digital analytics
  • Social marketing
  • Web experience management
  • Testing and targeting
  • Media optimization

Analytics

  • SiteCatalyst
  • Adobe Discover
  • Insight

Social

  • Adobe Social

Experience Manager

  • CQ
  • Scene7

Target

  • Test&Target
  • Recommendations
  • Search&Promote

Media Optimizer

  • AdLens
  • AudienceManager
  • AudienceResearch

Document services

  • Acrobat
  • EchoSign [opens in a new window]
  • FormsCentral [opens in a new window]
  • SendNow [opens in a new window]
  • Acrobat.com [opens in a new window]

Publishing

  • Digital Publishing Suite

  • See all products
Business solutions

By business need

  • Digital analytics
  • Digital publishing
  • Document management
  • Media optimization
  • Social marketing
  • Testing and targeting
  • Video editing and serving
  • Web development [opens in a new window]
  • Web experience management
  • See all business needs

By industry

  • Broadcast
  • Education
  • Financial services
  • Government
  • Publishing
  • Retail
  • See all industries
Support & Learning

I need help

  • Products
  • Adobe Creative Cloud
  • Adobe Marketing Cloud
  • Forums [opens in a new window]

I want to learn

  • Training and tutorials
  • Certification [opens in a new window]
  • Adobe Developer Connection
  • Adobe Design Center
  • Adobe TV [opens in a new window]
  • Adobe Marketing Center
  • Adobe Labs [opens in a new window]
Download
  • Product trials
  • Adobe Flash Player
  • Adobe Reader
  • Adobe AIR
  • See all downloads
Company
  • Careers at Adobe
  • Investor Relations
  • Newsroom
  • Privacy
  • Corporate Social Responsibility
  • Customer Showcase
  • Contact us
  • More company info
Buy
  • For personal and professional use
  • For students, educators, and staff
  • For small and medium businesses
  • Volume Licensing
  • Special offers
  • Adobe Marketing Cloud sales [opens in a new window]
Search
 
Info Sign in
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Welcome,
My Adobe
My orders
My information
My preferences
My products and services
Sign out
My cart
Privacy My Adobe
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out Privacy 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
Promotions
Estimated shipping
Tax
Calculated at checkout
Total
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
HTML5JavaScript
Was this helpful?
Yes   No

By clicking Submit, you accept the Adobe Terms of Use.

 
Thanks for your feedback.

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 CSS3 transitions: A comprehensive guide
  • The pursuit of simplicity
  • An Overview of Brackets' Code Architecture
  • Backbone.js Wine Cellar tutorial – Part 1: Getting started
  • JavaScript design patterns – Part 1: Singleton, composite, and façade
  • Getting started with PhoneGap in Eclipse for Android
  • Using the Geolocation API
  • Unit test JavaScript applications with Jasmine
  • Backbone.js Wine Cellar tutorial – Part 2: CRUD
  • Build a Hangman game with HTML5 Canvas, JavaScript, and CSS – Part 1: Creating the interface

Products

  • Adobe Creative Cloud
  • Creative Suite
  • Adobe Marketing Cloud
  • Acrobat
  • Photoshop
  • Digital Publishing Suite
  • Elements family
  • SiteCatalyst
  • For education

Download

  • Product trials
  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR

Support & Learning

  • Product help
  • Forums

Buy

  • For personal and professional use
  • For students, educators, and staff
  • For small and medium businesses
  • Volume Licensing
  • Special offers

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 © 2013 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy | Cookies

Ad Choices

Reviewed by TRUSTe: site privacy statement