Binding model with same id not working with backgrid in Backbone.js

108 Views Asked by At

I am binding a User model class to backgrid. User model contains id, name, domain. Our aim is display the users corresponding to different domains. Hence same user can be present in multiple domains. In fact we would have records with same id, name but different domain.

Hence while binding only the first record is displayed. How can I display the other records? Please note that in order to maintain uniqueness, I can add a recordId column. But I cannot make any change to existing id column. I noticed that if my id contains unique value, the issues gets resolved. But I cannot make any changes to Id column. Please help.

Divya

2

There are 2 best solutions below

1
Shaun_K On

It depends on how you have implemented the UI but you can create a new attribute for example 'data-id' and give it a unique value that's combination of id+domain. Then use the data-id for all manipulation the same way you would do for id. Ex: $([data-id='id+domain']) instead of $('#id')

0
vassiliskrikonis On

Well it's a bit of database theory but it's like having a one-to-many relationship across tables. It's true that the user id's appear multiple times but actually what should be unique here is the pair (user-id, domain-id).

So you could map your user collection to a let's say UsersWithDomains collection, where each record has a unique id and has user_id and domain_id as attributes.

To map your collection I suggest using the underscore methods which return back an array NOT so you'll need to keep that in mind not to forget to make them collections again.