Skip to content

Commit

Permalink
Allow setting global strings with a user.Ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
Splizard committed Feb 14, 2020
1 parent 624fec7 commit ad4428e
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions script/global/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package global
import (
"encoding/base64"
"math/big"
"strconv"

"github.com/qlova/script/language"
"github.com/qlova/seed/script"
"github.com/qlova/seed/user"

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

//SetFor is a set method that should be called whenever the parent value is set.
func (ref Reference) SetFor(u user.Ctx) {
u.Execute(`if (dynamic["` + ref.string + `"]) dynamic["` + ref.string + `"]();`)
}

//New returns a new globl variable reference.
func New(name ...string) Reference {
if len(name) > 0 {
Expand Down Expand Up @@ -70,6 +77,12 @@ func (s String) Set(q script.Ctx, value script.String) {
s.Reference.Set(q)
}

//SetFor the global.String to be the given value.
func (s String) SetFor(u user.Ctx, value string) {
u.Execute(`window.localStorage.setItem("` + s.string + `", ` + strconv.Quote(value) + `);`)
s.Reference.SetFor(u)
}

//Bool is a global Boolean.
type Bool struct {
Reference
Expand Down

0 comments on commit ad4428e

Please sign in to comment.