Ejemplo n.º 1
0
func main() {
	app := ng.NewModule("publicApp", []string{
		"ngCookies",
		"ngResource",
		"ngSanitize",
		"ngRoute",
	})
	app.NewController("HomeCtrl", func(scope *ng.Scope) {
		scope.Set("user", gSiteData.User())
	})
	app.NewController("UserCtrl", func(scope *ng.Scope, http *ng.HttpService) {
		user := gSiteData.User()
		if user == nil {
			http.Get(serverUrl("/auth")).Success(func(user biz.User, status int) {
				gSiteData.SetUser(user)
			})
		}
		scope.Set("username", user.Username)
	})

	app.Config(func(r *ng.RouteProvider) {
		r.When("/", ng.RouteOptions(
			ng.RouteController{"HomeCtrl"},
			ng.RouteTemplate{"views/home.html"},
		))
		r.When("/user", ng.RouteOptions(
			ng.RouteController{"UserCtrl"},
			ng.RouteTemplate{"views/user.html"},
		))
	})

	ng.AddHttpInterceptor(app, "authInterceptor", func(q *ng.QProvider, rootScope *ng.RootScope) ng.HttpInterceptor {
		return ng.HttpInterceptor{
			OnRequest: func(c *ng.ReqSpec) {
				token := gSiteData.User().Token
				if token != "" {
					c.Headers.Value["AuthToken"] = token
				}
			},
			OnResponse: func(j js.Object) interface{} {
				return q.NowOrLater(j)
			},
		}
	})
}
Ejemplo n.º 2
0
func main() {
	Describe("Test suite", func() {
		app := ng.NewModule("publicApp", []string{"ngMock", "ngRoute"})
		app.NewController("MainCtrl", func(scope *ng.Scope) {})
		app.Config(func(services *ng.Injector) {
			ng.RouteProvider(services).When("/", ng.Options(
				ng.Route.Controller("MainCtrl"),
				ng.Route.TemplatePath("test"),
			))
		}, ng.Inject("$routeProvider"))

		BeforeEach(func() {
			js.Global.Call("module", "publicApp")
		})
		js.Global.Call("it", "Route test", func() {
			ng.Bootstrap([]string{"publicApp"})
			js.Global.Call("inject", []interface{}{"$route", "$location",
				func(route js.Object, location js.Object) {
					location.Call("path", "/")
					Expect(route.Get("routes").Get("/").Get("templateUrl")).ToBe("test")
				}})
		})
	})
}