1

I have two tables: Question and UserProfile

My database is bring back a list of Question objects and each contains a createdBy which is an integer.

  • I have a userProfiles: [] array that contains { id, name } objects
  • I have a questions: [] array that contains { id, text, createdBy } objects

Is there some way I could link these two arrays on the client so that when I do a data-ng-repeat to go through the returned list of questions then I can get the name of the user rather than the createdBy which is just an integer number that references id in the userProfiles table?

I am just wondering "is this something that people do on the client" or "is this kind of linking always done on the server with a join". For me it seems like a waste to be doing it on the server when I already have an array of [] id, name on the client.

4

1 回答 1

1

Angular doesn't have much to do with this question. If you already have all the users at client side, it's indeed a good idea to simply get the user information directly from this array. If the array is small, you just need to loop through it until you find the user with the given ID. If it's large, consider creating a map which allows directly getting the user given its ID:

var usersById = {};
angular.forEach(users, function(user) {
    usersById[user.id] = user;
}

If your question is more "what should I do in general?", then I would say that, in general, you don't have the whole list of users at client side, because that list is much too large, so the join is made at server-side.

于 2013-07-05T20:25:39.273 回答