0

Yes I know there is a plugin called Backbone-Relational but I have a serious problem with its fetchRelated function which, in my opinion, renders it useless for me.

So I was wondering if there are any alternatives? Or do we even need plugins like Backbone-Relational at all? How would you handle following scenario with pure Backbone:

Let's say we have two Backbone models: Company and Person. A Company instance can have many Persons. So company.get('employees') will return an array of Person IDs. If I want to get details of related employees, I'll have to iterate over the array and fetch() each of the Persons from server. But what if those Person instances have been already downloaded? Is there a clean way to make sure that there is no redundancy?

May be we can maintain a Collection for each model and dump every instance we download into it. Then we can download an instance only when it is not present in the Collection. But I think it will make code look horrible.

So please share your experience guys. Thanks!

craftsman
  • 15,133
  • 17
  • 70
  • 86

1 Answers1

0

As you suggested, I would give my Company model a Persons attribute. But you seem to forget collections also have a fetch method (as well as many other methods you'd find pretty useful, as the get method).

Also, as a last thing, I'd like to quote Backbone's doc (regarding the fetch method of Collections):
"Note that fetch should not be used to populate collections on page load — all models needed at load time should already be bootstrapped in to place. fetch is intended for lazily-loading models for interfaces that are not needed immediately: for example, documents with collections of notes that may be toggled open and closed."

Loamhoof
  • 8,293
  • 27
  • 30
  • How does Collection#fetch help prevent multiple downloads of same instance? – craftsman Apr 04 '13 at 09:22
  • Sorry it seems I misunderstood a little bit. Is your collection changing that much? How is it changing? As an example, to avoid downloading the same model again and again with growing collection, I used to pass a parameter with the highest id when fetching data. Also, can a person be part of several collection? – Loamhoof Apr 04 '13 at 09:28
  • Sorry I couldn't get what you mean by collection changing? Does is it mean adding/removing persons into it? If that is the case, then yes, persons can be added/removed from it. Yes person can be part of several collections, and that is the case which makes it tricky I guess. Let's say we have a model City which can have many Persons. We have a City instance London which has Persons p1, p2, p3 and p4 (already fetched). We have a Company Virgin which has Persons p3, p4, p5 and p6. Now when we fetch persons for Company Virgin, it will re-fetch p3 and p4 (which were already fetched). – craftsman Apr 04 '13 at 09:54
  • As I stated, you could still pass the ids of the persons already fetched as a GET parameter, but it could get pretty ugly. I'll put more thought into it. – Loamhoof Apr 04 '13 at 09:56
  • @craftsman as the server has no idea what it has already sent (well, he could...), you really have only 2 solutions. Either fetch the models individually, or use a GET parameter to send the list of ids of the models already fetched. I really can't think of anything else. (thought about volatile collections, but the server wouldnt know of them ~~) – Loamhoof Apr 04 '13 at 12:39