Object Relations
Association Methods in Javascript
Objectives
Understand how to use associate objects with a store.
Understand how to use JavaScript methods to find associated objects.
Associating Objects
So far we have seen how to construct different types of objects in JavaScript with the class syntax.
Now let's say all of our users have many items they purchased. We can represent the items in the following way.
If we have a user with multiple items, we would want a way to associate those items with that user. To do so, first determine how an item is associated with a user. There are two types of relationships for us to choose from:
Many to Many - that is, an item has many users, and a user has many items
HasMany and BelongsTo - that is, a user has many items and an item belongs to a user.
So you could imagine representing the information in the following way:
However, there is the potential that two users may have the same name, so name is probably not the best way to identify a particular user. Instead, we should probably associate them by giving each user an id, and each item an id.
Let's try to see what's going on in the data structure above. We assign a variable called store
to a JavaScript object. The store
object will represent all of the objects that are initialized, and we will use it to store these objects. The store
object has two keys, each of which points to an array: one to represent the collection of items and one to represent the collection of users.
Let's see if we can answer some questions with our data structured like this. For example, if we want to see the name of the user that is associated with our first item, just take a look at the userId
which is 1, and then go find the user with id 1, and see that the name is Cindy. We can also go find all of the items associated with Cindy. To do so, we see that its id is 1, and then find all of the items with a userId
of 1: the first and second items.
So this is the structure we are aiming for. How do we hook this up to our classes?
Linking Instances to a Store
Assign an id each time we make a new instance
We need to assign each item object an id, and that id should increment each time we make a new item. I bet if you close your eyes and rub your forehead with your index finger, you can think of the solution yourself.
We set the itemId
outside of the Item
class so we can initialize the itemId
to zero only one time, and then increment every time an item is initialized. Notice that we use ++itemId
to increment the itemId
and then assign it to the new item's id.++itemId
is used instead of itemId++
because if you put the ++
before the variable name this will return the value after incrementing.
Our second task is to insert these new objects to the
store
Ok, let's do the same thing with users.
Finally, let's allow the ability to associate a user with an item.
So from the code above, you can see that we can associate an item with a user either by passing through an item to a user upon initialization or by calling a the setUser
setter method that we wrote.
Summary
In this lesson, we saw how we can use a plain javascript object to store and associate our data. We showed how we can assign each new instance an id by modifying our constructor
method. We also saw that we can write setters or modify our constructor methods to provide an interface to associate two objects.
Resources
Last updated