Managing Persistence with Callbacks
Let’s come up with an approach of using callbacks to manage persistence in Mastery.
Building persistence
Let’s build our persistence solution. We will write quite a bit of code to start with, including a second Response
model, but the benefits of this labor will be worth it. The second response will not be the same as the first because each response struct solves a different problem:
-
One captures user responses.
-
The other persists them to the database.
Using callbacks
We’ll manage persistence with callbacks, tweaking Mastery to take a function we’ll call when each new response is submitted. Here’s how the function will work:
-
In some solutions, perhaps with an online practice quiz, if we don’t need to save responses, that function will do nothing.
-
In others, that function will save responses to the database.
Get hands-on with 1300+ tech skills courses.