Chapter 16. Controllers: linking models and views
This chapter covers
- Acting on user input
- Updating models in response to user actions
- Passing updated models to views for display
- Completing the console version of The Crypt
Part 2 of Get Programming with JavaScript has been about organizing your code. As your programs grow, that organization pays dividends, making it easier to focus on individual pieces, switch modules to alter functionality, and reuse code in multiple projects.
Breaking programs into modules encourages you to give each module a specific task. Chapters 14, 15, and 16 form a trilogy, with each chapter looking at a common task a module might perform. You met models in chapter 14 and views in chapter 15 and now you link them with controllers.
To see controllers in action and how they act on user input to manage models and update views, you continue your work on two projects: the fitness app and The Crypt. In the fitness app, users will log sessions of exercise, and in The Crypt, players will face puzzles to solve as they explore perilous places. But will they escape before their health reaches zero?
Your team members have been telling friends and family about the fitness app you’re creating and they have a list of people lined up to test it out; they’re a super-keen bunch and have been diligently logging their exercise on paper. Your work has gone well so far—what’s left to do? Here are the requirements set out by the team: