0

I have a Model user set to be like this:

 User = Backbone.Model.extend({
      urlRoot: '/users',

      defaults: {
        id: '',
        username: '',
        password: ''
      },

      idAttribute: '_id',

      initialize: function () {
        console.log('User initiated');
      }

    });

And Collection to be like this:

var Users = Backbone.Collection.extend({

    url: "/users",

    model: User,

  });

  Users = new Users();

Then when I'm trying to get the models in my view using Users.fetch(), I'm only getting back one result in the "collection" array no matter how many results I have on the backend.

However, after I remove the idAttribute: '_id' from Model User, I get the exact number of results as the backend.

Does anyone know why this is happening?

p.s. As I went into the console, it seems that Users.fetch() was fired up the same amount of times as the number of users I have (each time the callback collection has only 1 model in it). So basically the last call overrides all previous ones and shows only the last result.

But I still have no idea what really is going on.

benjaminz
  • 3,118
  • 3
  • 35
  • 47

1 Answers1

0

This generally happens when your id attribute is undefined for all models, or otherwise the same value. Check that the value _id is defined and distinct in the response.

EDIT:

Are you sure you aren't trying to reference id without the underscore? If that's the case you can leave out the idAttribute property as id is the default value.

Ryan Lynch
  • 7,676
  • 1
  • 24
  • 33
  • I realized I put the "idAttribute: '_id' " in the wrong place, it should be inside the "defaults" brackets for model definition. Thanks though! – benjaminz May 11 '15 at 19:12