Marcy Lab School Docs
  • Welcome
  • Student Guidelines & Policies
    • Student Handbook
    • AI Policy
    • Academic Calendar
  • Environment Setup
    • Local Environment Setup - Mac
    • Local Environment Setup - Windows
    • GitHub Setup
    • Postgres Setup
  • How-Tos
    • How To Code at Marcy: Code Style Guide
    • How to Do Short Response and Coding Assignments
    • How to Debug
    • How to PEDAC
    • How to Create A GitHub Organization and Scrumboard
    • How to Create Projects with Vite
    • How to Deploy on GitHub Pages
    • How to Deploy on Render
    • How to Test your API with Postman
  • Mod 0 - Command Line Interfaces, Git, and GitHub
    • Overview
    • 1. Command Line Interfaces
    • 2. Git & GitHub
    • 3. Git Pulling & Merging
    • 4. Git Branching & PRs
  • Mod 1 - JavaScript Fundamentals
    • Overview
    • 1. Intro to Programming
    • 2. Errors
    • 3. Node & Node Modules
    • 4. Variables, Functions & String Methods
    • 5. Control Flow, typeof, and Math
    • 6. Loops
    • 7. Arrays
    • 8. Objects
    • 9. Higher Order Functions: Callbacks
    • 10. Higher Order Functions: Array Methods
    • 11. Regex
  • Mod 2 - HTML, CSS & the DOM
    • Overview
    • 1. HTML
    • 2. CSS
    • 3. Accessibility (a11y)
    • 4. The Document Object Model (DOM) API
    • 5. Events
    • 6. Forms
    • 7. The Box Model and Positioning
    • 8. Flexbox
    • 9. Grid & Media Queries
    • 10. ESModules
    • 11. Vite
    • 12. LocalStorage
  • Mod 3 - Async & APIs
    • Overview
    • 1. Promises
    • 2. Fetch
    • 3. Building a Fetching App
    • 4. Async & Await
    • 5. A Generic Fetch Handler
  • Mod 4 - Project Week!
    • Important How Tos and Guides
      • How to Create a GitHub Organization and Scrum Board
      • How To Start a Project with Vite
      • How To Deploy a Project with GitHub Pages
    • Project Week Overview
    • Agile Methodologies
    • Deliverables & Milestones
    • Technical Requirements Checklist
    • Free API List
    • Collaborative GitHub
  • Mod 5 - Object-Oriented Programming
    • Overview
    • 1. Intro to OOP, Encapsulation, Factory Functions, and Closure
    • 2. Classes
    • 3. Private & Static
    • 4. UML Diagrams & Has Many/Belongs To Relationships
    • 5. Challenge: Implementing Has Many/Belongs To
    • 6. Inheritance
    • 7. Polymorphism
    • 8. Review and Practice
    • MDN: Object Prototypes
  • Mod 6 - Data Structures & Algorithms
    • Overview
    • Important How Tos and Guides
      • How to Debug
      • How to PEDAC
    • 1. Nodes & Linked Lists
    • 2. Singly & Doubly Linked Lists
    • 3. Stacks & Queues
    • 4. Recursion
    • 5. Trees
  • Mod 7 - React
    • Overview
    • Important How Tos and Guides
      • How to Create Projects with Vite
      • How to Deploy on GitHub Pages
    • 1. Intro to React
    • 2. Events, State, and Forms
    • 3. Fetching with useEffect
    • 4. React Router
    • 5. Building a Flashcards App
    • 6. React Context
    • 7. Global Context Pattern
  • Mod 8 - Backend
    • Overview
    • Important How Tos and Guides
      • How to Deploy on Render
      • How to Test your API with Postman
      • Postgres Setup
    • 1. Intro to Express
    • 2. Building a Static Web Server with Middleware
    • 3. Securing API Keys and Environment Variables
    • 4. RESTful CRUD API
    • 5. Model-View-Controller Architecture
    • 6. SQL and Databases
    • 7. JOIN (Association) SQL Queries
    • 8. Knex
    • 9. Your First Fullstack App!
    • 10. Migrations & Seeds
    • 11. Schema Design & Normalization
    • 12. Hashing Passwords with Bcrypt
    • 13. React Express Auth Template Overview
  • Mod 9 - Civic Tech Hackathon
    • Overview
    • Rubric
  • Mod 10 - Capstone
    • Overview
Powered by GitBook
On this page
  • Overview
  • Build the Models from Scratch
  • Challenge: Build Your Own!
  1. Mod 5 - Object-Oriented Programming

5. Challenge: Implementing Has Many/Belongs To

Previous4. UML Diagrams & Has Many/Belongs To RelationshipsNext6. Inheritance

Last updated 3 months ago

Follow along with code examples !

Overview

The accompanying repo contains a Vite app that has been built out for you to implement a has many / belongs to relationship between classes.

The app allows users to maintain a list of Authors and Books by those authors.

In the provided repository, you will find a Vite project in the app/ directory. Inside, there is a src/ and src-solution/ folder. Currently, the index.html file links to the src-solution/.

See what the completed app does by doing the following:

  • Change directories into app

  • npm i

  • npm run dev

  • Play around with the app!

Build the Models from Scratch

To set up your project, return to index.html and change the script such that it loads the src/main.js file instead of the solution file.

Your task is to complete the Author and Book classes in the src/ folder.

Before you begin, read through the code and understand the structure.

  • The main.js file is the entry point. It already is making use of the Author and Book classes, even though they are incomplete. Read through the code in this file and make sense of what the file expects of the Author and Book class.

  • The models folder has two files, Author and Book.js, each defining a class. Book is exclusively used by Author while Author is used exlusively within main.js.

  • The utils folder has a getId.js file with an id-generating function for the classes above.

  • The render-functions.js file has functions for rendering the front end components. It doesn't interact with the classes at all. It just receives data from main.js to render. You do not need to touch this file.

Challenge: Build Your Own!

Once you've compelted the Author and Book classes, replace the classes in the models folder with classes of your own to demonstrate a new has many / belongs to relationship. You should also update the front-end code to utilize your new classes.

Examples of has many / belongs to classes include:

  • Doctor and Appointment

  • Playlist and Song

  • Class and Student

  • Team and Player

Have fun!

here
An application that allows a user to add authors and books by those authors to a collection.