Use default value for localstorage read when no stored value, fixes #1

This commit is contained in:
Mikko Ahlroth 2013-12-13 23:03:45 +02:00
parent 0971777d6c
commit 9e8d35ea2b
2 changed files with 7 additions and 3 deletions

View file

@ -12,7 +12,7 @@ function connect() {
return db; return db;
} }
function readSetting(db, key) { function readSetting(db, key, defVal) {
var setting = null; var setting = null;
db.readTransaction(function(tx) { db.readTransaction(function(tx) {
@ -32,6 +32,10 @@ function readSetting(db, key) {
else if (setting === 'false') { else if (setting === 'false') {
setting = false; setting = false;
} }
// If setting has never been read (doesn't exist), use default value
else if (setting === null) {
setting = defVal;
}
return setting; return setting;
} }

View file

@ -16,7 +16,7 @@ Dialog {
onOpened: { onOpened: {
Storage.db = Storage.connect(); Storage.db = Storage.connect();
var setVal = Storage.readSetting(Storage.db, "interval"); var setVal = Storage.readSetting(Storage.db, "interval", 15.0);
if (setVal !== undefined) { if (setVal !== undefined) {
intervalSlider.value = setVal; intervalSlider.value = setVal;
} }
@ -25,7 +25,7 @@ Dialog {
} }
timerEnable.checked = Storage.readSetting(Storage.db, timerEnable.checked = Storage.readSetting(Storage.db,
"timerEnable") "timerEnable", false)
} }
onAccepted: { onAccepted: {