[ 3 posts ]

jnicholas

  • Joined: Tue Sep 27, 2011 1:28 pm
  • Posts: 10
  • Offline
  • Profile

Models with attributes that are also models

Post Posted: Tue May 29, 2012 2:29 pm
+0-
I'm unclear on best practices for handling model that has other models as an attribute. For example, if I have a model of a person

ATTRS: {
name: {value: null},
age: {value: null},
city: {value: null},
state: {value: null},
}

and I want to have a array or similar collection as one of the attributes. Something like books with its own ATTRS of title, author, publisher or vacations with ATTRS of location,date,rating. In the case I'm modeling now the sub models are only used in by the main model but I can see other scenarios where they are also used independently (students and teachers, players and teams etc)

Would I create a ModelList and use it in the ATTRS section? Make custom getter/setters that handled an array? Handle it all with methods outside of the ATTRS manually?

Patrick Ouellette

  • Username: hendross
  • Joined: Tue Feb 14, 2012 8:36 am
  • Posts: 10
  • Offline
  • Profile

Re: Models with attributes that are also models

Post Posted: Wed May 30, 2012 10:26 am
+0-
You need to break it down a bit further...I have 4 pieces.

+Book Model & VIew
This will define the data structure of a book, title, author, genre. The view will be coupled to the model and determine how an individual book is rendered in the list. e.g <li>

+Book Model-list
This is pretty simple, but can support some helper filter methods e.g genre()

+Lastly a Book Shelf View which will contain a book model list.
This is the highest level view, it will instantiate your Book List which provides acess to this.bookList.add()

If you want more flexibility, a book shelf would be a model and you could have a list of this which
would be appropriot for a say, Library View? OR model and list of libraries.....you get the idea.

Example
http://jsfiddle.net/hendross/QDehF/11/

jnicholas

  • Joined: Tue Sep 27, 2011 1:28 pm
  • Posts: 10
  • Offline
  • Profile

Re: Models with attributes that are also models

Post Posted: Thu May 31, 2012 2:53 pm
+0-
Thanks, that was very helpful
  [ 3 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum