Skip to main content

Backbone.js the Basics: How to create simple apps in JavaScript

Backbone is a JavaScript front-end framework that is used to solve down the purpose of modular programming in JavaScript. Whenever a programmer starts to build nice apps in JavaScript with some functionality the code starts to become more and more cumbersome with more and more functions working with one another and returning values from one function to other. Backbone provides a platform that makes the code of the application more standardized by using some build in methods and structures of writing code.
I decided to work on this framework because one of the project on which I was working had it's code written in this platform and it was whole lot of code( somewhat around 45000 lines of code). After opening the main file, sometimes my editor starts to hang. Here is the code of that project: https://github.com/singh1114/converse.js
Backbone doesn't works like many of the MVC( Models, Views, Controllers) frameworks like ruby on rails. The code of backbone consist of four major classes:
  • Models
  • Views
  • Collections
  • Controllers
Sometimes Routers are also considered as the main part but if you want to build a SPA( single page application) then you don't have to think of them.
Dependencies
Backbone has a soft dependency on jquery and hard dependency on Underscore.js. So, if you want to build a application then create a folder with your app name and then download the newest production version of these three softwares and then add them as a script in the order as follows :
  • Jquery
  • underscore.js
  • backbone.js
See an example here which includes CDN format of these files included:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js">
<script type="text/javascript" src="https://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js">
<script type="text/javascript" src="https://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js">
Now let's talk about all the major things in Backbone one by one:
  • Models
Models are the single row in the database in this case. Each time you create a model in the app you create a set of rules in which the user can provide some data to the app.

var Person = Backbone.Model.extend({
initialize: function(){
console.log("This works as constructor");;
},
defaults:{
Name: 'Ranvir Singh',
Age: 21,
}
});
var sahil = new Person({Name: "Sahil", Age: 23});
var ranvir = new Person();
var name = sahil.get('Name');
document.write(name);
var sahil.set({Name: 'Sahil Sharma'}) ;
sahil.save();
Person is the type of class which has a initialize function and some default values which tells us what kind of data can be arrive from some place. There are many functions like get and set which can be used to provide further functionalities. You can find some of them on this link.
get and set functions are used so that we have less probability to change the values by chance. save function is used to permanently change the changes to the particular.
We will discuss the rest part in next tutorial and I will tell you when it's out.

Comments

Popular posts from this blog

140 Character : No More Limit

Is Twitter really removing the 140 character limit.           Hopefully Twitter is really going to remove this hurdle in front of there users by removing the 140 character limit in June at least from the private messages but limit for the tweet is going to stay as it is.          According to a website  seventeen.com  the new limit is going to be set upon 10000 characters.  The set up is taking place and is going to be out and ready to be use in July. Why the 140 character limit was there?           In the early years when twitter was not in the picture and was not yet started some undergraduates thought of starting a SMS service which then grew  into today's Twitter. As the SMS do have constraints about the number of characters Twitter came out with having a 140 character limits. Why no limit now?           Twitter had removed the limit now to keep up with there competitors like Whats App and Face Book. Both of them have no limits on number of chara

Short Stories

This page is dedicated to all the fictional short stories that are written on this blog. i have collected the whole list at one place for you. So, if you are fiction lover then you will love to go through them. Please go through them and tell me what I should be doing to improve them.  I might had done a mistake in writing or there may be some problem. Tell me what it is and I will try to solve them. I have done a lot of hard work in writing all of them please be generous to like and share with your friends. Army man turned into a thief for his son Steve came out of the hospital in hurry and asked for a taxi. He had to wait for little long as it was 5 in the morning and no cab was there for him. He did not waited for that long and began to take long steps toward his final destination. 5 minutes later after he started the walk, he found a cab. He got into the cab and asked him to take him straight to the pub.                                                       ...Rea

My story for Chetan Bhagat

Write India campaign for Chetan Bhagat ended on 30th of August. So the time for sending the stories to TOI are over now( Not Literally, Check this post for more information). Also the results for the preface of Amish are out. As you know that I am not a very good writer, therefore I was not able to get my name into the winning 3's(Hahahaha). The stories of the winners are going to be in the book which is the part of this campaign. I would love to read that book when it is out. But I will not loose my hope and keep trying till the competition ends. It does not cost anything whether I win or not as I have this blog to share my stories with my readers. I will keep posting my stories here and keep asking my readers to tell me what is the area in which I need to improve. Finally I am going to end up in a better condition. Similarly all the other guys who are willing to post their stories up on this blog can improve too. Let me tell you how? All of the guys who want to post o