JavaScript Backbonejs - How can I print the results of a fetch?

Hi I'm new to Backbone and I was just playing around with it a little, here is my code:

    var Users = Backbone.Collection.extend ({
        url : 'http://backbonejs-beginner.herokuapp.com/users'
    });

    var users = new Users();
    users.fetch({
        success: function () {
            console.log(users);
        }
    });

The fetch call succeeds and I am returned with an object that looks like:

[
  {
    "id": "hqel839m1071rbggsxf7",
    "firstname": "Thomas",
    "lastname": "Davis",
    "age": 12
  }
]

How can I print different parts of the result? For example I want to print the "id" parameter of the first item. Can I iterate it like an array?

I tried to do console.log(users[0].id) but it doesn't work.

Thanks.

Answer:1

Not to forget about the arguments passed to success callback of collection.fetch which are (collection, response, options). Check documentation here. You can use the collection argument to select a particular model. Check below code :

var Users = Backbone.Collection.extend ({
    url : 'http://backbonejs-beginner.herokuapp.com/users'
});

var users = new Users();
users.fetch({
    success: function (collection, response, options) {
        //Will log JSON objects of all User objects
        console.log(collection.toJSON());
        //You can get a Model using 'id'
        var user = collection.get("hqesig1ea1br2k6horay");
        // Will log User Model for id "hqesig1ea1br2k6horay"
        console.log(user);
        //You can then fetch Model attributes this way
        console.log("ID: ", user.get('id'));
        console.log("First name: ", user.get('firstname'));
        console.log("Lastname : ", user.get('lastname'));
    }
});

A fiddle for your reference.

Answer:2

I am tyring to append different autocomplete list results in one text box. It is easy to give different text box for different autocomplete results. html : <input id="university" name="...

I am tyring to append different autocomplete list results in one text box. It is easy to give different text box for different autocomplete results. html : <input id="university" name="...

I'm trying to convert a simple string to a unix timestamp using moment.js moment('2014-01-14 07:25 PM').unix(); moment('2014-01-14 07:25 AM').unix(); The problem is I get the same result with AM or ...

I'm trying to convert a simple string to a unix timestamp using moment.js moment('2014-01-14 07:25 PM').unix(); moment('2014-01-14 07:25 AM').unix(); The problem is I get the same result with AM or ...

  1. convert string to simpledateformat
  2. convert simpledateformat to date
  3. converting a simple data type into a class

This is my html code : <input type="file" nv-file-select="" options="{ photoType: 'studentPic' }" uploader="uploader" /> This is angular code : var uploader = $scope.uploader = new ...

This is my html code : <input type="file" nv-file-select="" options="{ photoType: 'studentPic' }" uploader="uploader" /> This is angular code : var uploader = $scope.uploader = new ...

  1. nervgh angular-file-upload examples
  2. nervgh/angular-file-upload
  3. https://github.com/nervgh/angular-file-upload

I'm looking at switching from resource to restangular in my AngularJS app. I like the new model, but I have one problem. I can't figure out how to make this API call: http://localhost:8000/api/entity/...

I'm looking at switching from resource to restangular in my AngularJS app. I like the new model, but I have one problem. I can't figure out how to make this API call: http://localhost:8000/api/entity/...

  1. restangular multiple base url
  2. restangular multiple requests
  3. multiple restangular
  4. restangular post multiple parameters