Google News
Backbone Interview Questions
In Backbone.js, by default, collections are not explicitly sorted. We can sort the collections by defining a comparator on the collection object. By defining a comparator, a collection is sorted whenever a model is added or the "sort()" method is invoked on a collection :
Example :
var Fruits = Backbone.Collection.extend({  
    comparator: function(a, b) { /* .. */ }  
// Or  
var Fruits = Backbone.Collection.extend({})  
var fruits = new Fruits()  
fruits.comparator = function(a, b) { /* .. */ }  
The comparator property can be a function with one argument or two arguments (similarly used in "sort"), or a string identifying the attribute by name to sort on.
When an attribute of a model in a collection changes, the collection doesn't sort itself. In this case, the sort must be invoked explicitly to update the order of models in the collection.
Backbone.sync is a function that is Backbone js call every time it attempts to read or save a model to the server.By default, it uses jQuery.ajax to make a RESTful JSON request and returns a jqXHR
Example Code :
Backbone.sync = function(method, model) {
  alert(method + ": " + JSON.stringify(model));
  model.set('id', 1);

var author= new Backbone.Model({
  author: "Sharad",
  website: ""
});;{author: "FTL"});


If you are working with Backbone, Backbone models follow traditional REST. As mentioned in the docs for Backbone. Sync () :

* Create → POST /collection
* Read → GET /collection [/id]
* Update → PUT /collection/id
* Patch → PATCH /collection/id
* Delete → DELETE /collection/id

If you need to modify what verbs and endpoints are used for methods fetch (), save (), and destroy (), you can override Backbone.sync on a per model basis to accomodate custom endpoints and HTTP verbs.
A list of some prominent features or key points of Backbone.js:
Backbone.js has a hard dependency with underscore.js, making it more functional and providing support for a range of useful collection-based operations.

Backbone.js has a soft dependency on jQuery.

Backbone.js is based on MVC architecture. When the model changes, it can update the HTML of your application automatically.

Backbone.js uses a client-side rendering framework or Javascript templating to render HTML, enabling you not to embed HTML code inside JavaScript code.

Backbone.js offers a significantly clean and elegant way for UI updates and DOM manipulations.
Generally, Backbone.js is required in the following conditions :
* When we have to develop a web application that requires a lot of JavaScript codes.
* It is also required if your application needs to be scalable and give a structure to your code.
* Backbone is required when a web application works with jQuery to traverse the DOM or give animations.
It is not recommended to change the "el" property of a view directly because when you attempt to change the "el" property directly, it may lead to inconsistent behavior from the Backbone.js view. The reason behind this inconsistent behavior is that when you change the "el" property directly, it doesn't automatically update the cached jQuery object property corresponding to it, which is "$el".
The correct way to do this is to use "setElement()" on the view.
Syntax :
Whenever you change some data within the model, the model objects fire the "change" event. However, the object fires another event with a name specific to the change attribute : "change:[attribute]".
For example :
var Fruit = Backbone.Model.extend({})  
var fruit = new Fruit({  
    weight: 3.25  
fruit.on('change:weight, function() {  
    // Event "change:weight" will fire whenever the weight attribute of fruit changes.  


In Backbone.js, the model.cid is a special property of models, the cid or client id, which is automatically assigned to all models when they are first created. The model.cid works as a unique identifier. This is very useful when the model is not saved to the server, but you want to show it on the UI.
The main function of to JSON in Backbone.js is to return a shallow copy of the model's attribute for JSON stringification. It does not return a JSON string and provides persistence, serialization, and augmentation before being sent to the server.
The Backbone.js attributes define a model's property and use the set() method to update the attributes. It is usually a form of the JSON object representing the model data on the server.
Syntax :

Example :
<script type="text/javascript">    
  var  Person = Backbone.Model.extend();    
  var person = new Person();    
  person.set({ name: "Chanti"});