YouTube Icon

Interview Questions.

Top 15 Backbone.js Interview Questions and Answers - May 20, 2022

fluid

Top 15 Backbone.js Interview Questions and Answers

Q1. Could You at any point Give An Example Of How To Build A Collection?
var Song = Backbone.Model.extend({
        defaults: {
            name: "Not indicated",
            craftsman: "Not indicated"
        },
        instate: function(){
            console.log("Music is the wer");
        }
    });

    var Album = Backbone.Collection.extend({
        model: Song
    });

    var song1 = new Song({ name: "How Bizarre", craftsman: "OMC" });
    var song2 = new Song({ name: "Sexual Healing", craftsman: "Marvin Gaye" });
    var song3 = new Song({ name: "Talk It Over In Bed", craftsman: "OMC" });

    var myAlbum = new Album([ song1, song2, song3]);
    console.log( myAlbum.models );//[song1, song2, song3]

Q2. Make sense of What Is Backbone.js Collections?
An arranged arrangement of models are addressed by Backbone.js assortments. Any occasion in model will set off an occasion in assortment straightforwardly. For instance, you can tie "change" occasion to be advised for a situation when any model in the assortment has been altered.

Q3. Make sense of What Is Model.cid?
Model.cid functions as a one of a kind identifier. It is a unique property of models, the cid or client id is naturally relegated to all models when they are first made. This property is helpful when the model isn't saved to the server, yet should be noticeable in the UI. It takes the from c1,c2… .

Q4. What Is Dynamic Routing?
If there should be an occurrence of Dynamic Routing, you can involve factors in the course. For instance, you should recover a post with a variable id with a well disposed URL string. You can indicate variable name in the course as :variablename in powerful directing.

<script>
    var AppRouter = Backbone.Router.extend({
        courses: {
            "posts/:id": "getPost",
            "*actions": "defaultRoute"//Backbone will attempt to match the course above first
        }
    });
    // Launch the switch
    var app_router = new AppRouter;
    app_router.on('route:getPost', work (id) {
        // Note the variable in the course definition being passed in here
        alert( "Get post number " + id );
    });
    app_router.on('route:defaultRoute', work (activities) {
        alert( activities );
    });
    // Begin Backbone history an important stage for bookmarkable URL's
    Backbone.history.start();
</script>

Q5. What Are The Configuration Options Available?
The setup choices accessible are:

InitialCopyDirection
modelSetOptions
change Triggers
boundAttribute
suppressThrows
converter
Q6. Make sense of What Is The Function Of Parse?
Whenever a model's information is returned by the server, in get and save , this information is called parse. It is called by Backbone at whatever point an assortment's models are returned by server, in bring.

Q7. Make sense of What Is Converter In Backbone.js?
A capacity is called when model's property is duplicated to a html component or when a html component esteem is replicated into a model's quality, this capacity is alluded as Converter in Backbone.js

Q8. Make sense of What Is Backbone.js Router Is Used For?
Whenever an application need to change their URL section to give bookmarkable and shareable URLs to an Ajax weighty application, backbone.js switch is utilized.

Q9. Make sense of When You Require Backbone.js?
Backbone.js is expected in following condition:

While fostering a web application that requires a ton of JavaScript
 It is required when you need to give design to your code, in the event that your application should be versatile.
Spine is valuable when a web application needs to work with jQuery to cross the DOM or give livelinesss.
Q10. What Is The Function Of Tojson?
It returns a shallow duplicate of the model's trait for JSON stringification. This capacity is utilized for ingenuity, serialization and for increase prior to being shipped off the server. This doesn't return a JSON string.

Q11. How Might You Track Any Change In Model Attribute Value?
All credits of a model can have audience members bound to them to recognize changes to their qualities. In our instate work, we will tie a capacity call everytime we change the worth of our trait. For this situation, if the name of our "individual" transforms, we will alarm their new name. Punctuation for appending callback to change occasion of trait is this.on("change", function(model){});.

See the model underneath:

Individual = Backbone.Model.extend({
        defaults: {
            name: 'Embryo',
            age: 0
        },
        instate: function(){
            alert("Below we are joining callback to change occasion of 'name' quality");
            this.on("change:name", function(model){
                var name = model.get("name");//'Stewie Griffin'
                alert("Changed my name to " + name );
            });
        }
    });

    var individual = new Person({ name: "Thomas", age: 67});
    person.set({name: 'Stewie Griffin'});//This triggers a change and will caution()

Q12. How Do You Define View In Backbone.js?
You can characterize sees in Backbone.js like Backbone models. See model beneath:

SearchView = Backbone.View.extend({
        instate: function(){
            alert("WOW! SearchView has been defined.");
        }
    });

    // The instate work is constantly called while starting up a Backbone View.
    // Think of it as the constructor of the class.
    var search_view = new SearchView();
    //You can determine el property for the view or, more than likely it will make void div and allocate it
<div id="search_container"></div>
var search_view = new SearchView({ el: $("#search_container") });

Q13. What Is Collection In Backbone.js?
Spine assortments are basically an arranged arrangement of models.

Commonly, your assortment will just utilize one sort of model however models themselves are not restricted to a kind of assortment.

var Song = Backbone.Model.extend({
      introduce: function(){
          console.log("Music is the wer");
      }
  });

  var Album = Backbone.Collection.extend({
    model: Song
  });

Q14. How Backbone Decides If It Should Use Post/get/Request To The Server? What Are The Methods Backbone Has Reserved For These Operations?
In the event that we start up a model with an id, Backbone.js will naturally play out a GET solicitation to the urlRoot + '/id' utilizing get() technique. (adjusting to RESTful shows)

Assuming the id characteristic of the model is invalid, Backbone.js will send a POST solicitation to the urlRoot of the server utilizing save() technique.

On the off chance that the id characteristic of the model isn't invalid, Backbone.js will send a PUT demand rather than a POST demand utilizing save() technique.

Assuming a model has an id we realize that it exists on the server, so on the off chance that we wish to eliminate it from the server we can call obliterate() technique. obliterate will shoot a DELETE/client/id (adjusting to RESTful shows).

See the model beneath for this multitude of above tasks:

// Here we have set just the 'id' of the model so it will call bring() technique.
    var client = new Usermodel({id: 1});

    // The bring beneath will perform GET/client/1
    // The server ought to return the id, name and email from the information base
    user.fetch({
        victory: work (client) {
            alert(user.toJSON());
        }
    })

// Here we have set every one of the properties, alongside id,
// of the model so it will call save() strategy with PUT
    var client = new Usermodel({
        id: 1,
        name: 'Thomas',
        email: 'myemailid@domain.com'
    });

    // How about we change the name and update the server
    // Since there is 'id' present, Backbone.js will fire
    // PUT/client/1 with a payload of '{name: 'Davis', email: 'myemailid@domain.com'}'
    user.save({name: 'Davis'}, {
        victory: work (model) {
            alert(user.toJSON());
        }
    });

// Here we have set every one of the properties of model.
    var client = new Usermodel({
        id: 1,
        name: 'Thomas',
        email: 'thomasalwyndavis@gmail.com'
    });

    // Since there is 'id' present, Backbone.js will fire
    // Erase/client/1
    user.destroy({
        victory: work () {
            alert('Destroyed');
        }
    });

Q15. How The Actions Performed On Model Is Trlated To Restful Operations? Give An Example.?
Models are utilized to address information from your server and activities you perform on them will be trlated to RESTful tasks. The id property of a model recognizes how to find it on the information base typically planning to the proxy key.

Assume you have a table Users with sections id, name, email and you need to save the model back on waiter when client clicks save button. Assuming the id property of the model is invalid, Backbone.js will send a POST solicitation to the urlRoot of the server. For this,

see the model underneath:

var UserModel = Backbone.Model.extend({
        urlRoot: '/client',//RESTful API relative way
        defaults: {
            name: '',
            email: ''
        }
    });
    var client = new UserModel();
    // Notice that we haven't set an 'id'. If there should arise an occurrence of update activity, we want to pass 'id' too.
    var userDetails = {
        name: 'Thomas',
        email: 'youemailid@domain.com'
    };
    // Since we have not set a 'id' the server will call
    // POST/client with a payload of {name:'Thomas', email: 'youremailid@domain.com'}
    // The server ought to save the information and return a reaction containing the new 'id'
    user.save(userDetails, {
        victory: work (client) {
            alert(user.toJSON());
        }
    })




CFG