Skip to content

Commit ad4428e

Browse files
committed
Allow setting global strings with a user.Ctx
1 parent 624fec7 commit ad4428e

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

script/global/global.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package global
33
import (
44
"encoding/base64"
55
"math/big"
6+
"strconv"
67

78
"github.com/qlova/script/language"
89
"github.com/qlova/seed/script"
10+
"github.com/qlova/seed/user"
911

1012
Javascript "github.com/qlova/script/language/javascript"
1113
)
@@ -33,6 +35,11 @@ func (ref Reference) Set(q script.Ctx) {
3335
q.Javascript(`if (dynamic["` + ref.string + `"]) dynamic["` + ref.string + `"]();`)
3436
}
3537

38+
//SetFor is a set method that should be called whenever the parent value is set.
39+
func (ref Reference) SetFor(u user.Ctx) {
40+
u.Execute(`if (dynamic["` + ref.string + `"]) dynamic["` + ref.string + `"]();`)
41+
}
42+
3643
//New returns a new globl variable reference.
3744
func New(name ...string) Reference {
3845
if len(name) > 0 {
@@ -70,6 +77,12 @@ func (s String) Set(q script.Ctx, value script.String) {
7077
s.Reference.Set(q)
7178
}
7279

80+
//SetFor the global.String to be the given value.
81+
func (s String) SetFor(u user.Ctx, value string) {
82+
u.Execute(`window.localStorage.setItem("` + s.string + `", ` + strconv.Quote(value) + `);`)
83+
s.Reference.SetFor(u)
84+
}
85+
7386
//Bool is a global Boolean.
7487
type Bool struct {
7588
Reference

0 commit comments

Comments
 (0)