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).
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
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
| 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 |
| Trying to Tame the Tablet | May 08, 2013 |
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- RSS Feeds
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- New Products
- Tech Tip: Really Simple HTTP Server with Python
- Connecting Android device to desktop Linux via USB
8 min 48 sec ago - Find new cell phone and tablet pc
1 hour 6 min ago - Epistle
2 hours 35 min ago - Automatically updating Guest Additions
3 hours 44 min ago - I like your topic on android
4 hours 30 min ago - Reply to comment | Linux Journal
4 hours 51 min ago - This is the easiest tutorial
11 hours 6 min ago - Ahh, the Koolaid.
16 hours 44 min ago - git-annex assistant
22 hours 44 min ago - direct cable connection
23 hours 7 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