-
Notifications
You must be signed in to change notification settings - Fork 6
/
angularjs.go
80 lines (60 loc) · 1.93 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
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{scope})
}})
}
type Scope struct{ *js.Object }
func (s *Scope) Apply(f func()) {
s.Call("$apply", f)
}
func (s *Scope) EvalAsync(f func()) {
s.Call("$evalAsync", f)
}
type JQueryElement struct{ *js.Object }
func (e *JQueryElement) Prop(name string) *js.Object {
return e.Call("prop", name)
}
func (e *JQueryElement) SetProp(name, value interface{}) {
e.Call("prop", name, value)
}
func (e *JQueryElement) On(events string, handler func(*Event)) {
e.Call("on", events, func(e *js.Object) {
handler(&Event{Object: e})
})
}
func (e *JQueryElement) Val() *js.Object {
return e.Call("val")
}
func (e *JQueryElement) SetVal(value interface{}) {
e.Call("val", value)
}
type Event struct {
*js.Object
KeyCode int `js:"keyCode"`
}
func (e *Event) PreventDefault() {
e.Call("preventDefault")
}
func NewModule(name string, requires []string, configFn func()) *Module {
return &Module{js.Global.Get("angular").Call("module", name, requires, configFn)}
}
func ElementById(id string) *JQueryElement {
return &JQueryElement{js.Global.Get("angular").Call("element", js.Global.Get("document").Call("getElementById", id))}
}
func Service(name string) *js.Object {
return js.Global.Get("angular").Call("element", js.Global.Get("document")).Call("injector").Call("get", name)
}
type HttpService struct{}
var HTTP = new(HttpService)
func (s *HttpService) Get(url string, callback func(data string, status int)) {
future := Service("$http").Call("get", url)
future.Call("success", func(data string, status int, headers *js.Object, config *js.Object) {
callback(data, status)
})
future.Call("error", func(data string, status int, headers *js.Object, config *js.Object) {
callback(data, status)
})
}