forked from neelance/go-angularjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angularjs.go
148 lines (104 loc) · 2.78 KB
/
angularjs.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package angularjs
import "github.com/gopherjs/gopherjs/js"
type Module struct{ *js.Object }
func (m *Module) NewController(name string, constructor func(scope *Scope)) {
m.Call("controller", name, js.S{"$scope", func(scope *js.Object) {
constructor(&Scope{Object: scope})
}})
}
func (m *Module) Factory(name string, cls interface{}) error {
var err error
arg, err := buildParams(cls, "ajs-service")
if err != nil {
println("Error building factory params: ", err.Error(), "factory: ", name)
return err
}
m.Call("factory", name, arg)
return nil
}
func (m *Module) Controller(name string, cls interface{}) error {
var err error
arg, err := buildParams(cls, "ajs-service")
if err != nil {
println("Error building controller params: ", err.Error(), "controller: ", name)
return err
}
m.Call("controller", name, arg)
js.Global.Set(name, js.MakeFunc(makeConstructor(cls)))
return nil
}
func (m *Module) Config(cb interface{}) error {
if err := funcReturn(cb, nil); err != nil {
return err
}
arg, err := buildParams(cb, "ajs-provider")
if err != nil {
println("Error building config params:", err.Error())
return err
}
m.Call("config", arg)
return nil
}
func (m *Module) Run(cb interface{}) error {
if err := funcReturn(cb, nil); err != nil {
return err
}
arg, err := buildParams(cb, "ajs-service")
if err != nil {
println("Error building run params:", err.Error())
return err
}
m.Call("run", arg)
return nil
}
func (m *Module) Value(name string, val interface{}) error {
m.Call("value", name, val)
return nil
}
func (m *Module) Filter(name string, val interface{}) error {
if err := funcReturn(val, []interface{}{""}); err != nil {
return err
}
if err := m.Value(name, val); err != nil {
return err
}
m.Call("filter", name, js.S{name, func(v interface{}) interface{} {
return v
}})
return nil
}
func (m *Module) Directive(name string, cb interface{}) error {
arg, err := buildParams(cb, "ajs-service")
if err != nil {
println("Error building directive params: ", err.Error(), "directive:", name)
return err
}
m.Call("directive", name, arg)
return nil
}
type Scope struct {
*js.Object `ajs-service:"$scope"`
}
func (s *Scope) Apply(f func()) {
s.Call("$apply", f)
}
func (s *Scope) EvalAsync(f func()) {
s.Call("$evalAsync", f)
}
type Event struct {
*js.Object
KeyCode int `js:"keyCode"`
}
func (e *Event) PreventDefault() {
e.Call("preventDefault")
}
var angularModule *js.Object
func NewModule(name string, requires []string) *Module {
// defer println("Failed to require:", recover())
// require := js.Global.Get("require")
// if require != js.Undefined {
// println("Requiring angular...")
// angularModule = require.Invoke("angular")
// }
return &Module{js.Global.Get("angular").Call("module", name, requires)}
}