I'm using the module pattern to build a small quiz in a modal, which is nearly complete. There is a button at the end of the quiz that says "try again" that will restart the quiz, and I'm having a hard time thinking of a DRY way to reset the settings property (a list of vars for the app) to its default value.
This what I have right now:
jQuery(function($) {
var s,
QuizApp = {
settings: {
timer: null,
isPaused: false,
count: null,
correctAnswer: null,
score: 0,
questionCounter: 1
},
init: function() {
s = this.settings;
this.questionsGet();
this.bindUIActions();
},
// Code omitted for event handlers and such
resetQuiz: function() {
s.timer = null;
s.isPaused = false;
s.count = null;
s.correctAnswer = null;
s.score = 0;
s.questionCounter = 1;
},
},
}; $(function() {
QuizApp.init();
});
});
Not very DRY - pretty bad actually. What I want is:
jQuery(function ($) {
var s,
QuizApp = {
settings: {
timer: null,
isPaused: false,
count: null,
correctAnswer: null,
score: 0,
questionCounter: 1,
defaults: { /* pseudo code to store settings here */ }
},
resetQuiz: function () {
s = s.defaults // pseudo code to reset the settings to the value stored in defaults
},
};
$(function () {
QuizApp.init();
});
});
Or something better, if you can think of it. I've done tons of searching for "reset object properties" and "clone object properties," but I've found nothing that explains how to do this. jQuery is ok to use, obviously.
Furthermore, I don't understand how/where to store the data and how to map the data back in because I don't fully understand what type of data this is (is "settings" an object property, and it's values sub-properties? Or is it a multi-dimensional array? What do I store the data in to retrieve its default values later? etc), so any kind of in-depth explanation or links to info of the data types we're working with here is an added bonus!