-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.go
47 lines (41 loc) · 1.19 KB
/
example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"encoding/json"
"github.com/drekle/go/rss/gopherjs/actions"
"github.com/drekle/go/rss/gopherjs/components"
"github.com/drekle/go/rss/gopherjs/dispatcher"
"github.com/drekle/go/rss/gopherjs/store"
"github.com/drekle/go/rss/gopherjs/store/model"
"github.com/gopherjs/gopherjs/js"
"github.com/gopherjs/vecty"
)
func main() {
attachLocalStorage()
vecty.SetTitle("GopherJS • TodoMVC")
vecty.AddStylesheet("node_modules/todomvc-common/base.css")
vecty.AddStylesheet("node_modules/todomvc-app-css/index.css")
p := &components.PageView{}
store.Listeners.Add(p, func() {
p.Items = store.Items
vecty.Rerender(p)
})
vecty.RenderBody(p)
}
func attachLocalStorage() {
store.Listeners.Add(nil, func() {
data, err := json.Marshal(store.Items)
if err != nil {
println("failed to store items: " + err.Error())
}
js.Global.Get("localStorage").Set("items", string(data))
})
if data := js.Global.Get("localStorage").Get("items"); data != js.Undefined {
var items []*model.Item
if err := json.Unmarshal([]byte(data.String()), &items); err != nil {
println("failed to load items: " + err.Error())
}
dispatcher.Dispatch(&actions.ReplaceItems{
Items: items,
})
}
}