From 6aa87e4b8168c7b1f45fadbc41ef479909792b52 Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Sat, 22 Apr 2017 10:45:42 +0200 Subject: [PATCH] todoapp: use local storage properly --- examples/todoapp/todoapp.nim | 54 +++++++++++++++++++----------------- src/jstrutils.nim | 2 ++ src/localstorage.nim | 10 +++++++ 3 files changed, 40 insertions(+), 26 deletions(-) create mode 100644 src/localstorage.nim diff --git a/examples/todoapp/todoapp.nim b/examples/todoapp/todoapp.nim index 1ba06a6..802bfc9 100644 --- a/examples/todoapp/todoapp.nim +++ b/examples/todoapp/todoapp.nim @@ -1,5 +1,5 @@ -import vdom, karax, karaxdsl, jstrutils, components +import vdom, karax, karaxdsl, jstrutils, components, localstorage type Filter = enum @@ -10,36 +10,35 @@ var filter: Filter entriesLen: int -proc getItem(key : cstring ): cstring {.importc: "localStorage.getItem"} -proc setItem(key, value : cstring ) {.importc: "localStorage.setItem"} -proc clearEntries() {.importc: "localStorage.clear"} +const + contentSuffix = cstring"content" + completedSuffix = cstring"completed" + lenSuffix = cstring"entriesLen" -proc getEntryCstring(pos: int): cstring = - result = getItem(&pos & cstring"cstring") +proc getEntryContent(pos: int): cstring = + result = getItem(&pos & contentSuffix) if result == cstring"null": result = nil proc isCompleted(pos: int): bool = - var value = getItem(cstring($pos & "bool")) + var value = getItem(&pos & completedSuffix) result = value == cstring"true" -proc setEntryCstring(pos: int, value: cstring) = - setItem(cstring($pos & "cstring"), value) +proc setEntryContent(pos: int, content: cstring) = + setItem(&pos & contentSuffix, content) -proc markAsCompleted(pos: int, value : bool) = - var val = cstring"true" - if not value: - val = cstring"false" - setItem(cstring($pos & "bool"), val) +proc markAsCompleted(pos: int, completed: bool) = + setItem(&pos & completedSuffix, &completed) -proc addEntry(str: cstring, bval : bool) = - setEntryCstring(entriesLen, str) - markAsCompleted(entriesLen, bval) +proc addEntry(content: cstring, completed: bool) = + setEntryContent(entriesLen, content) + markAsCompleted(entriesLen, completed) inc entriesLen + setItem(lenSuffix, &entriesLen) -proc updateEntry(pos: int, str: cstring, bval: bool) = - setEntryCstring(pos, str) - markAsCompleted(pos, bval) +proc updateEntry(pos: int, content: cstring, completed: bool) = + setEntryContent(pos, content) + markAsCompleted(pos, completed) proc onTodoEnter(ev: Event; n: VNode) = addEntry(n.value, false) @@ -54,7 +53,7 @@ proc editHandler(ev: Event; n: VNode) = proc focusLost(ev: Event; n: VNode) = selectedEntry = -1 proc editEntry(ev: Event; n: VNode) = - setEntryCstring(n.key, n.value) + setEntryContent(n.key, n.value) selectedEntry = -1 proc toggleEntry(ev: Event; n: VNode) = @@ -62,12 +61,12 @@ proc toggleEntry(ev: Event; n: VNode) = markAsCompleted(id, not isCompleted(id)) proc onAllDone(ev: Event; n: VNode) = - clearEntries() + clear() selectedEntry = -1 proc clearCompleted(ev: Event, n: VNode) = for i in 0..