vrouter

package module
v0.0.0-...-21c993f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 12, 2019 License: MIT Imports: 6 Imported by: 0

README

vrouter

router for vecty

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	// Params is the parameters from the url as a map of names to values.
	Params map[string]string
	// Path is the path that triggered this particular route. If the hash
	// fallback is being used, the value of path does not include the '#'
	// symbol.
	Path string
	// InitialLoad is true iff this route was triggered during the initial
	// page load. I.e. it is true if this is the first path that the browser
	// was visiting when the javascript finished loading.
	InitialLoad bool
	// QueryParams is the query params from the URL. Because params may be
	// repeated with different values, the value part of the map is a slice
	QueryParams map[string][]string
}

Context is used as an argument to Handlers

type Handler

type Handler func(ctx *Context)

Handler is a function which is run in response to a specific route. A Handler takes a Context as an argument, which gives handler functions access to path parameters and other important information.

type Router

type Router struct {

	// ShouldInterceptLinks tells the router whether or not to intercept click events
	// on links and call the Navigate method instead of the default behavior.
	// If it is set to true, the router will automatically intercept links when
	// Start, Navigate, or Back are called, or when the onpopstate event is triggered.
	ShouldInterceptLinks bool
	// ForceHashURL tells the router to use the hash component of the url to
	// represent different routes, even if history.pushState is supported.
	ForceHashURL bool
	// Verbose determines whether or not the router will log to console.log.
	// If true, the router will log a message if, e.g., a match cannot be found for
	// a particular path.
	Verbose bool
	// contains filtered or unexported fields
}

Router is responsible for handling routes. If history.pushState is supported, it uses it to navigate from page to page and will listen to the "onpopstate" event. Otherwise, it sets the hash component of the url and listens to changes via the "onhashchange" event.

func New

func New() *Router

New creates and returns a new router

func (*Router) Back

func (r *Router) Back()

Back will cause the browser to go back to the previous page. It has the same effect as the user pressing the back button, and is just a wrapper around history.back()

func (*Router) CanNavigate

func (r *Router) CanNavigate(path string) bool

CanNavigate returns true if the specified path can be navigated by the router, and false otherwise

func (*Router) HandleFunc

func (r *Router) HandleFunc(path string, handler Handler)

HandleFunc will cause the router to call f whenever window.location.pathname (or window.location.hash, if history.pushState is not supported) matches path. path can contain any number of parameters which are denoted with curly brackets. So, for example, a path argument of "users/{id}" will be triggered when the user visits users/123 and will call the handler function with params["id"] = "123".

Example
package main

import (
	"fmt"
	"github.com/pubgo/vrouter"
)

func main() {
	// Create a new Router object
	r := vrouter.New()
	// Use HandleFunc to add routes.
	r.HandleFunc("/greet/{name}", func(context *vrouter.Context) {
		// The handler for this route simply grabs the name parameter
		// from the map of params and says hello.
		fmt.Printf("Hello, %s\n", context.Params["name"])
	})
	// You must call Start in order to start listening for changes
	// in the url and trigger the appropriate handler function.
	r.Start()
}
Output:

func (r *Router) InterceptLinks()

InterceptLinks intercepts click events on links of the form <a href="/foo"></a> and calls router.Navigate("/foo") instead, which triggers the appropriate Handler instead of requesting a new page from the server. Since InterceptLinks works by setting event listeners in the DOM, you must call this function whenever the DOM is changed. Alternatively, you can set r.ShouldInterceptLinks to true, which will trigger this function whenever Start, Navigate, or Back are called, or when the onpopstate event is triggered. Even with r.ShouldInterceptLinks set to true, you may still need to call this function if you change the DOM manually without triggering a route.

func (*Router) Navigate

func (r *Router) Navigate(path string)

Navigate will trigger the handler associated with the given path and update window.location accordingly. If the browser supports history.pushState, that will be used. Otherwise, Navigate will set the hash component of window.location to the given path.

func (*Router) Start

func (r *Router) Start()

Start causes the router to listen for changes to window.location and trigger the appropriate handler whenever there is a change.

func (*Router) Stop

func (r *Router) Stop()

Stop causes the router to stop listening for changes, and therefore the router will not trigger any more router.Handler functions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL