CSS

Backbone.js - MVC framework for front-end javascript

What is Backbone.js?


Backbone is a framework for front-end JavaScript, unlike jQuery focus on easier DOM manipulation and event binding, backbone provide a structure for separating data model and DOM, just like a MVC framework separate model, view and controller. Make heavy JavaScript application easier to develop and maintain.

Why Backbone?


In jQuery, we will write a sequence of chain selector for assign data model to DOM element like:

var article = {
  author:"Joe",
  content: "testing"
};
$('#article').click(function(event){
  $(this).find('.content').text(article.content);
});
For backbone, it provide the Model class and View class, which is the main two element of backbone.
var Article = Backbone.Model;

var article = new Article({
  author:"Joe",
  content: "testing"
});

var ArticleView = Backbone.View.extend({
  el: $('#article'),
  initialize: function(){
    _.bindAll(this,'updateContent');
    this.model.bind('change',this.updateContent);
  },
  events: {
    "click .content" : "updateContent"
  },
  updateContent: function(){
    this.$('.content').text(this.model.get("content"));
  }
});

var articleView = new ArticleView({model:article});
OK, it's seen like a completely overwork, that's my first thought when I see a clientside MVC framework too, but we have to dig further to understand why people create it.

Event Binding


First, it separate the data model and view - which include event handle and DOM element, we can easily bind data model in view like:
this.model.bind('change',this.updateContent);
It means when the data model change, the view will be automatically updated to DOM element.

Also, backbone provide literal event binding:
events:{
  "click .content": "updateContent"
}
which bind the "click" event to ".content" class, trigger "updateContent" function in current view.
It's useful when binding multiple events.

Restful synchronization


Furthermore, the backbone provide a RESTful format API for synchronize data model with server.
every model class in backbone have a corresponding url for synchronize, and when the "model.save" or "model.fetch" method is called, the model will make an Ajax request to update data with server.
For example:
article.save({author:"John"});
will first find article.sync method, if undefined, call Backbone.sync.
The sync method will make an ajax request to server, and update model, trigger event and update to view.

the url of model is defined by RESTful API:

  • create -> POST /model
  • read -> GET /model[/id]
  • update -> PUT /model/id
  • delete -> DELETE /model/id

and we can also change it by overwrite url parameter

Dependency


backbone.js depend on underscore.js and jQuery/Zepto library, since it is only a 3.9k framework focus on the structure of JavaScript application.

Collection and Controller

backbone provide Collection, which is the collection of Backbone.Model,
the Controller in backbone is more like the route map in MVC framework, it provide clientside fragment url routing, eg. "/#article/12" match "article(id)". So the url can be recorded and bookmarked by browser.

Template

In Backbone, we can create template in view.
In that way we can add and remove multiple view and render with template.
The underscore.js provide template engine or we can use other engine too.

In view:
render:function(){
    var template = "
<%=content %>
"; $(this.el).html( this.template(this.model.toJSON()) ); return this; }

Conclusion


JavaScript MVC framework is good for rich client application and also can keep your javascript cleaner.
But whether to use it depend on how much script in your application, for small page, the MVC is total overhead, but it is good for large scale page like Mail or Desktop like application.

More

Backbone
Sample todo application
Annotated source

[Note] Start first instance on Amazon Web Service EC2

How-to guide by Amazon
http://docs.amazonwebservices.com/AWSEC2/latest/GettingStartedGuide/

Amazon have become the largest cloud service company that provide compute instance and stroage
in their data center, that we can rent those computing resource as instances.

What's EC2?
Elastic Compute Cloud, the amazon computing service, we can rent an instance and run service on it as a virtual machine.

Amazon also have other service like S3 - simple storage service , is a storage service that you can upload file and let other people download. S3 provide auto bitTorrent support for big files.

Start an Instance

Login Amazon Aws console , choose EC2 tag, press "launch instance button"

1. Choose Instance Type: AMI(amazon machine image)
Default has Amazon custom Linux, Suse Linux and Windows Server 2003, both 32bit and 64bit.
There's also community AMI you can choose (which have CentOS and Ubuntu)

Read ubuntu document to find official AMI for ubuntu: https://help.ubuntu.com/community/EC2StartersGuide

ami-06ad526f for free micro instance supported Ubuntu 11.04 , check the Star mark for free support or not.
The root device ebs means the data will be saved in ebs service by default, which will not disappear after shutdown,

2. Select number of instance, instance type (related to money, micro is free)

3. Advance Instance Option: kernel, ramdisk id, monitoring, import user data shutdown behavior
you can import your userdata as text or file, that's useful when setting up multiple instances, you don't have to set user separately.

4.key-value tag to administrate multiple instances, and help to manage
ex: Owner = Jimmy
Admin = Jimmy
Service = Redmine
Stack = Development

5.choose key-pair to login with ssh , pem format (see below for how to login)

6.choose security group
Decided which incoming network traffic should be delivered to your instance
- access web traffic on port 80
- Its like the firewall setting, need to add every open port for usage (black-listed)
- SSH:22, windows RDP(Remote Desktop Protocol)
- source : 0.0.0.0/0 means no restriction on ip/submask
- 192.168.2.0/24 means restriction on 192.168.2 sub-area

How to login?


pem: public key of a specific certificate. Apache use this kind of certificate.
In apache installs, this frequently resides in /etc/ssl/servercerts.
it is also called X.509 Certificates

terminal:
# ssh -i ~/.ssh/XXX.pem root@ec2-##-##-##-##.compute-1.amazonaws.com

connection in windows using Putty:
generate private key from .pem file using puttygen,
choose Load => XXX.pem
then save private key XXX.ppk


Connection with putty under windows
choose connection => auth => load private key


More on aws:

EC2 command line tool: using command line to manage EC2 Instance
need to apply X.509 keypair for Amazon EC2 AMI Tools,
Amazon Web Services > Your Account > Security Credentials > X.509 Certificate