4

I have the following situation and I think the best way to deal with it is to use Backbone relation.
Please correct me if there is another solution.

I have one collection (1) and one model (2).
The collection result looks like (3) and the model result like (4).

Finally the view should look like this (5).

My questions are:
1) possible to use Backbone relation to handle this situation?
2) If yes, how should I rewrite the FeedsCollection to take automatically the needed data from the UserModel?


(1)

// FeedsCollection
var FeedsCollection = Backbone.Collection.extend({
    url: "http://localhost/feeds"
});

(2)

// User Model
var UserModel = Backbone.Model.extend({
    url: "http://localhost/user"
});

(3) feedsCollection result

//feedsCollection.toJSON(); 

[
   {id:1, message: "something one", creator_id: 100},
   {id:2, message: "something two", creator_id: 101},
]

(4) userModel result

userModel = new UserModel({id: 100});
userModel.fetch();
userModel.toJSON(); // {id:100, name: "jhon"}

userModel = new UserModel({id: 101});
userModel.fetch();
userModel.toJSON(); // {id:101, name: "herry"}

(5) Finally the view result should be like:

[
    {message: "something one", creator_id: 100, name: "jhon"},
    {message: "something two", creator_id: 101, name: "herry"},
]
Derick Bailey
  • 72,004
  • 22
  • 206
  • 219
js999
  • 2,063
  • 4
  • 26
  • 34
  • Was (2) meant to be var UserModel = Backbone.Model.extend({}) as opposed FeedsCollection? I am confused to how the FeedsCollection is related to the UserModel? Are they related based on the property creator_id? – TYRONEMICHAEL May 24 '12 at 06:47
  • @TyroneMichael, thanks for your comment, I did correct the (2), Yes they are related by `creator_id`. `feedsCollection.creator_id = userModel.id` – js999 May 24 '12 at 06:58
  • Is this a one to many | many to one relationship or a many to many? (E.g. Can users have multiple feeds and feeds have multiple users?) – jmk2142 Jun 05 '12 at 02:19

1 Answers1

0

Have you considered returning the user's name from the server directly. So that the url "/feeds", would return:

[
    {message: "something one", creator_id: 100, name: "jhon"},
    {message: "something two", creator_id: 101, name: "herry"},
]

In case you just want the user's name to show near the message this seems a simple solution. I don't know what's your server's backend, but I'm sure this can be done efficiently.

So answering you're questions:

1) possible to use Backbone relation to handle this situation?

Probably you could use the plugin to solve this situation, but I wold not recommend introducing a plugin to solve this specific example. To me seems inefficient to query the server for user models, just to display their name. However in different situations this might be appropriate.

Anyway in case you decide to load user models with plugin or without it. You have to ask some questions to figure out if its worth it:

  1. How many queries will this generate? One per each user might be too much.
  2. How many user models will the server return in case you load all with one request? All users might be to much. So you need to load only needed ones.
  3. How much data will the server return? Do you really need all users's attributes?

So in conclusion I would still argue you return the users's name directly from the server and load a specific user model only when you need more than it's name.

Andreyy
  • 511
  • 2
  • 11