Rich Internet Apps That Just Work—Writing for the User
Next, we need to declare what all the states are in which the application can exist. These will determine what paths we want. In our contacts app, we really have only two states: 1) listing the contacts and 2) viewing one particular contact (while the main list remains open).
In keeping with RESTful style, let's declare our URLs as follows:
1) Listing the contacts: contacts.html#/contacts.
2) Viewing one particular contact: contacts.html#/contacts/:id (where :id is replaced by the ID of the viewed contact).
In addition, we want a default path. What happens if the user just opens contacts.html?
3) Default path: contacts.html, re-routed to contacts.html#/contacts.
Notice something interesting. We are defining various declarative paths. When each of these paths is encountered, we want to take a certain action. Essentially, these are routes. Most Ruby-based frameworks (Sinatra, Rails, Merb/Rails3 and so on) use this exact language, as does Sammy.
So, we have three routes and their actions:
contacts.html→redirect to contacts.html#/contacts.
contacts.html#/contacts→list contacts.
contacts.html#/contacts/:id→show details for contact :id.
In our included JavaScript file contactapp.js, we declare each of the routes:
var app = $.sammy(function(){
// for the verb GET with the path #/, go to #/contacts
this.get("#/",function(context){
this.redirect("#/contacts");
});
// for the verb GET with the path #/contacts, render the contacts
this.get("#/contacts",function(context){
// get our contact list from the server
$.get("/contacts",function(res,status) {
// render the results - should include
// status-checking for safety
// jQuery already parsed the response to JSON for us
var list = res, tr, td, table = $("#list table"), a;
// clear the existing list
table.empty();
// use jQuery to go through each result
$.each(list,function(i,elm) {
tr = $("<tr></tr>").appendTo(table);
td = $("<td></td>").appendTo(tr);
// the key part: make it a URL
a = $("<a></a>").attr("href","#/contacts/"+elm.id).text
↪(elm.lastName + " " + elm.firstName).appendTo(td);
});
},"json");
// hide the details
$("#details table").hide();
});
// for the verb GET with a specific path #/contacts/:id,
// render that one contact
this.get("#/contacts/:id",function(context){
// get our contact list from the server - access
// param :id as this.params.id
$.get("/contacts/"+this.params.id,function(res,status) {
// render the results - should include
// status-checking for safety
// jQuery already parsed the response to JSON for us
var contact = res, table = $("#details table");
// find the elements in the table, and fill them with the data
$("#firstName",table).text(contact.firstName);
$("#lastName",table).text(contact.lastName);
$("#email",table).text(contact.email);
// make sure the table is shown
table.show();
},"json");
});
});
// set up a default route for contacts.html
$(function(){
app.run("#/");
});
Notice several key elements:
There are no event handlers here at all. Although we might need some for things like edit buttons or key presses, navigation in the app really happens using URL <a> links. This makes it really easy to manage the app and understand what every change does. Clicking on a contact in the list is clicking on a URL. We just happen to use that URL to control our app.
We could have used a handler just as well. Instead of using an <a> link, we could have put on a handler with $("list td").click(function(e),{...});.
This application is incredibly short and easy to understand. That is the beauty of Sammy.
The browser URL changes, but the page does not reload. We remain in the Rich Internet App world, yet browser semantics simply work: Back, Forward, Reload. Try it!
The full sample, without minified JS, is available on-line (see Resources).
Sammy gives us the power to provide Rich Internet Applications simultaneously, work with the user's mindset rather than against it, and program our apps using routes declaratively, making it much simpler to build yet richer Internet applications.
The Sammy library is open source under the MIT license and available on-line (see Resources).
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
Web Development News
Developer Poll
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- RSS Feeds
- A Topic for Discussion - Open Source Feature-Richness?
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Readers' Choice Awards
- The Secret Password Is...
- All the articles you talked
1 hour 41 min ago - All the articles you talked
1 hour 44 min ago - All the articles you talked
1 hour 45 min ago - myip
6 hours 10 min ago - Keeping track of IP address
8 hours 1 min ago - Roll your own dynamic dns
13 hours 15 min ago - Please correct the URL for Salt Stack's web site
16 hours 26 min ago - Android is Linux -- why no better inter-operation
18 hours 41 min ago - Connecting Android device to desktop Linux via USB
19 hours 10 min ago - Find new cell phone and tablet pc
20 hours 8 min ago








Comments
Helpful in current software development
thanks for the article, we could just it in a current development project.
http://uws-software-service.com/home.html