Interview :: Backbone
Backbone.js event can be defined as the module which can be mixed with any object. There are following methods which are used to manipulate Backbone.js events,
- on - It binds an event to an object and executes the callback whenever an event is fired.
- off - It removes callback functions or all events from an object.
- trigger - It invokes the callback functions for the given events.
- once - It extends the backbone model class while creating your own backbone model.
- listenTo - It informs one object to listen to an event on another object.
- stopListening - It can be used to stop listening to events on the other objects.
- listenToOnce - It causes the listener, to occur only once before the callback function is being removed.
A view is the important part of the Backbone.js architecture. In a Backbone.js application, a view is responsible for the end user interface. The view defines the way in which the application looked at the user. The View is also responsible for listening to the events and reacting to them accordingly.
Model Binder is a class which is used to bind the model and the view together. The binding is done to archive synchronization.
Some most robust functionalities of the model binder are
- It allows the developer to define the scope when binding is created using J-Query.
- In some of the cases, we can rely on the default scoping rules which are based on the name attribute of HTML.
- The scoping rules can be redefined if the views are complicated.
Advantages of Backbone.js:
- You can develop a web application with Backbone.js by using JavaScript with the minimal set of data-structuring (models & collections) and user interface (views & URLs).
- It is best for developing MVC like web applications, single page web applications or complex JavaScript web applications in an organized and structured manner without JavaScript code mixing with HTML.
- It provides API with many functions.
- It provides a key-value binding and custom events.
- It facilitates you to abstract your data into models and your DOM application into views and binds the two together using events.
The Converter is a function which is used to convert the JavaScript object to a model. It is invoked when the transition is made between an HTML element and the model's attribute.
Sync is a function that is called every time. It attempts to read or save a model to the server. It persists the state of the model to the server.
Two methods can be used to manipulate the Backbone.js utility:
- Backbone.noConflict: It returns the Backbone objects to its original value and provides a facility to store the reference to a backbone. It can be used to embed the backbone on third-party websites, where you don't want to thrash the existing backbone.
- Backbone.$: This property is used when you have multiple copies of jQuery on the page or want to tell Backbone to use a particular object as its DOM / Ajax library.
Unbinding function is used to remove the bindings on the model.
There are the following configuration options available in Backbone.js.
- modelSetOptions
- boundAttributes
- supressThrows
- converter
- change Triggers
- InitialCopyDirection