-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmapping.go
41 lines (35 loc) · 944 Bytes
/
mapping.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
package vue
import (
"strings"
"github.com/gopherjs/gopherjs/js"
"github.com/oskca/gopherjs-json"
)
// FromJS set the corresponding VueJS data model field from obj
// new data model field will be created when not exist
func (v *ViewModel) FromJS(obj *js.Object) *ViewModel {
for _, key := range js.Keys(obj) {
// skip internal or unexported field
if strings.HasPrefix(key, "$") || strings.HasPrefix(key, "_") {
continue
}
v.Object.Set(key, obj.Get(key))
}
return v
}
func (v *ViewModel) FromJSON(jsonStr string) *ViewModel {
return v.FromJS(json.Parse(jsonStr))
}
func (v *ViewModel) ToJS() *js.Object {
obj := js.Global.Get("Object").New()
for _, key := range js.Keys(v.Object) {
// skip internal/unexported field
if strings.HasPrefix(key, "$") || strings.HasPrefix(key, "_") {
continue
}
obj.Set(key, v.Get(key))
}
return obj
}
func (v *ViewModel) ToJSON() string {
return json.Stringify(v.ToJSON())
}