1

i'm novice in backbonejs and i'm doing some project that includes fetching and displaying friends list. for this project i'm using parse.com as database. but i'm stocked at this point.

for example: i have following data's in user and friends models.

var user = [
{
    id: 'x1',
    firstname: 'Ashik',
    lastname: 'shrestha',
    phone: '12321321',
    mobile: '123213',
    email: 'xyz@gmail.com'
},
{
    id: 'x2',
    firstname: 'rokesh',
    lastname: 'shrestha',
    phone: '12321321',
    mobile: '123213',
    email: 'rokesh@gmail.com'
},
];

var friends = [
{
    user_id: 'x1',
    user_friend_id: 'x2'
},
{
    user_id: 'x1',
    user_friend_id: 'x4'
},
{
    user_id: 'x1',
    user_friend_id: 'x10'
},
{
    user_id: 'x2',
    user_friend_id: 'x25'
}

];

// collections

var userCollection = Backbone.collection.extend({
model: user
});

var friendListCollection = Backbone.collection.extend({
model: friends
});

var friends = new friendListCollection();

now what i want?

when i fetch friends collection object, i want to get friends list of user with their details.

example::

 friends.fetch({
success: function(ob){
    var ob =ob.toJSON(); 
    // i want ob to be like 
    [

        {
            id: 'x2',
            firstname: 'rokesh',
            lastname: 'shrestha',
            phone: '12321321',
            mobile: '123213',
            email: 'rokesh@gmail.com'
        },
        {
            id: 'x4',
            firstname: 'rokesh',
            lastname: 'shrestha',
            phone: '12321321',
            mobile: '123213',
            email: 'rokesh@gmail.com'
        },
        {
            id: 'xx10',
            firstname: 'rokesh',
            lastname: 'shrestha',
            phone: '12321321',
            mobile: '123213',
            email: 'rokesh@gmail.com'
        },
    ]
    }
});

should i create new collection to relate them or is there any other way to do this??

Thanks in advance!

krozero
  • 5,929
  • 3
  • 21
  • 33

2 Answers2

0

To use the least of server requests to gain a better performance and less presure on server side, I would suggest you to add this logic on your server-side rather than here on client-side. e.g. When fetching with parameters like ?detail=true, the server then return simple information with detailed data, otherwise only return simple information.

If you have a good reason to seperate them into different Collections, you have to fetch those collections consequently.

dotslashlu
  • 3,361
  • 4
  • 29
  • 56
0

Assuming you do not wish to change your data structure, you can use BackboneJS' model's idAttribute property, to retrieve a specific model from a collection by a specific key, usually an "id".

When you define your model, you should also define the idAttribute for the model, which will later allows your to access it from the collection, by the value of this field.

When a Backbone collection is synced, all models are parsed according to their defined structure, adding administrative functionality on top of their data.

Consider the following example:

var myModel = Backbone.Model.extend({
  idAttribute: "id"
  ...
});

var myCollection = Backbone.Collection.extend({
  model: myModel
  ...
});

Once myCollection holds one or more "myModel"(s) you can then simply use the following:

var myModelFromMyCollection = myCollection.get(id);

the idAttribute of the model can by any of the model's fields...

For your use case, lets assume both friendListCollection and userCollection are already available and have models in them, consider the following code to get the full details of each friend from it's user model like so:

  friendListCollection.each(function(friendModel) {
     var friendFullDetailsFromUsersCollection = userCollection.get(friendModel.id);
     console.log(friendFullDetailsFromUsersCollection);
     ...
  });
Eyal Ronel
  • 11
  • 3