angular-ui / AngularJS-StyleGuide

Example of scalable architecture for my NG-Conf 2014 talk
http://www.youtube.com/watch?v=Iw-3qgG_ipU
284 stars 49 forks source link

Handling model associations #9

Closed szimek closed 10 years ago

szimek commented 10 years ago

How do you handle model associations like one-to-one (e.g. user has one profile) and one-to-many (e.g. user has many posts)?

Do you manually overwrite properties with instances of proper class, e.g. like this:

class User
  constructor: (params) ->
    object = angular.extend(@, params)
    object.profile = new Profile(user.profile)
    object.posts = user.posts.map (post) -> new Post(post)

do you have a smarter way of doing it or you don't do anything like this at all?

ProLoser commented 10 years ago

Honestly, I'd do whatever suits you best but if it was me and you are probably going to access the object via the user then I'd probably just store it as a profile property, yes. Your user.getProfile() would populate this property for you.

ProLoser commented 10 years ago

Also keep in mind I don't know how important it is to keep a 1 to 1 match of your bqxkend structure. If you find that just having a user object and api is all you need and you just have a property that is big it might not be worth it to manage spinning up profile objects. You probably don't need the verbosity in your frontend of user.profile.save() when you could simply have user.save() distribute the profile data on the backend.

szimek commented 10 years ago

Yeah, you're probably right. Thanks!