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

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

Preface of write India campaign by Ashwin Sanghi

Starting of another month and coming of new writer for you. That is the main rule of Write India campaign. So, who is this time... Yes you are write its the most leading writer of India Mr. Ashwin Sanghi So, Ashwin Sanghi's preface is live for you on write India campaign's website. Lots of stories have been written for Mr. Chetan Bhagat as the competition went to its second month in the month of August. But hey, that is the story of last month and we never think about the past. We are the people who love to stay in the present, Aren't we.  So, let us talk about the present. Let us talk about the current writer who is on the board and is ready with his preface for you to write stories for him and let us write with a believe that we are going to win this time.  By the way their is no bad in reading the past as it is better to see what happened earlier and then come up with a new strategy for yourself so you can learn from the mistakes of others. So here I am l

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