bind

package
v0.300.1 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2026 License: MIT Imports: 7 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrValueNotSettable = errors.New("value not settable")

Functions

This section is empty.

Types

type BindClickedHook

type BindClickedHook[T comparable] func(bind Binder[T], elem *jaws.Element, name string) (err error)

BindClickedHook is a function to call when a click event is received.

The Binder locks are not held when the function is called.

type BindGetHook

type BindGetHook[T comparable] func(bind Binder[T], elem *jaws.Element) (value T)

BindGetHook is a function that replaces JawsGetLocked for a Binder.

The lock will be held before calling the function, preferring RLock over Lock, if available. Do not lock or unlock the Binder in the function. Do not call JawsGet.

The bind argument is the previous Binder in the chain, and you probably want to call it's JawsGetLocked first.

type BindSetHook

type BindSetHook[T comparable] func(bind Binder[T], elem *jaws.Element, value T) (err error)

BindSetHook is a function that replaces JawsSetLocked for a Binder.

The lock will be held before calling the function, preferring RLock over Lock, if available. Do not lock or unlock the Binder in the function. Do not call JawsSet.

The bind argument is the previous Binder in the chain, and you probably want to call it's JawsSetLocked first.

type BindSuccessHook

type BindSuccessHook func(*jaws.Element) (err error)

BindSuccessHook is a function to call when a call to JawsSet returns with no error.

The Binder locks are not held when the function is called.

Success hooks in a Binder chain are called in the order they were registered. If one of them returns an error, that error is returned from JawsSet and no more success hooks are called.

type Binder

type Binder[T comparable] interface {
	RWLocker
	Setter[T]
	jtag.TagGetter
	Formatter
	jaws.ClickHandler

	JawsBinderPrev() Binder[T] // returns the previous Binder in the chain, or nil
	JawsGetLocked(elem *jaws.Element) (value T)
	JawsSetLocked(elem *jaws.Element, value T) (err error)

	// SetLocked returns a Binder[T] that will call fn instead of JawsSetLocked.
	//
	// The lock will be held at this point.
	// Do not lock or unlock the Binder within fn. Do not call JawsSet.
	//
	// The bind argument to the function is the previous Binder in the chain,
	// and you probably want to call it's JawsSetLocked first.
	SetLocked(fn BindSetHook[T]) (newbind Binder[T])

	// GetLocked returns a Binder[T] that will call fn instead of JawsGetLocked.
	//
	// The lock will be held at this point, preferring RLock over Lock, if available.
	// Do not lock or unlock the Binder within fn. Do not call JawsGet.
	//
	// The bind argument to the function is the previous Binder in the chain,
	// and you probably want to call it's JawsGetLocked first.
	GetLocked(fn BindGetHook[T]) (newbind Binder[T])

	// Success returns a Binder[T] that will call fn after the value has been set
	// with no errors. No locks are held when the function is called.
	// If the function returns an error, that will be returned from JawsSet.
	//
	// The function must have one of the following signatures:
	//  * func()
	//  * func() error
	//  * func(*Element)
	//  * func(*Element) error
	Success(fn any) (newbind Binder[T])

	// FormatHTML returns a HTMLGetter using fmt.Sprintf(f, JawsGet[T](elem))
	FormatHTML(f string) (getter HTMLGetter)

	// Clicked returns a Binder[T] that will call fn when JawsClick is invoked.
	//
	// The Binder locks are not held when the function is called.
	Clicked(fn BindClickedHook[T]) (newbind Binder[T])
}

func New

func New[T comparable](l sync.Locker, p *T) Binder[T]

New returns a Binder[T] with the given sync.Locker (or RWLocker) and a pointer to the underlying value of type T.

The pointer will be used as the UI tag.

type Formatter

type Formatter interface {
	// Format returns a Getter[string] using fmt.Sprintf(f, JawsGet[T](elem))
	Format(f string) (getter Getter[string])
}

type Getter

type Getter[T comparable] interface {
	JawsGet(elem *jaws.Element) (value T)
}

func MakeGetter

func MakeGetter[T comparable](v any) Getter[T]

func StringGetterFunc

func StringGetterFunc(fn func(elem *jaws.Element) (s string), tags ...any) Getter[string]

StringGetterFunc wraps a function and returns a Getter[string]

type HTMLGetter

type HTMLGetter interface {
	JawsGetHTML(e *jaws.Element) template.HTML
}

A HTMLGetter is the primary way to deliver generated HTML content to dynamic HTML nodes.

func HTMLGetterFunc

func HTMLGetterFunc(fn func(elem *jaws.Element) (tmpl template.HTML), tags ...any) HTMLGetter

HTMLGetterFunc wraps a function and returns a HTMLGetter.

func MakeHTMLGetter

func MakeHTMLGetter(v any) HTMLGetter

MakeHTMLGetter returns a HTMLGetter for v.

Depending on the type of v, we return:

  • HTMLGetter: `JawsGetHTML(e *Element) template.HTML` to be used as-is.
  • Getter[string]: `JawsGet(elem *Element) string` that will be escaped using `html.EscapeString`.
  • Formatter: `Format("%v") string` that will be escaped using `html.EscapeString`.
  • fmt.Stringer: `String() string` that will be escaped using `html.EscapeString`.
  • a static `template.HTML` or `string` to be used as-is with no HTML escaping.
  • everything else is rendered using `fmt.Sprint()` and escaped using `html.EscapeString`.

WARNING: Plain string values are NOT HTML-escaped. This is intentional so that HTML markup can be passed conveniently from Go templates (e.g. `{{$.Span "<i>text</i>"}}`). Never pass untrusted user input as a plain string; use template.HTML to signal that the content is trusted, or wrap user input in a Getter or fmt.Stringer so it will be escaped automatically.

type RWLocker

type RWLocker interface {
	sync.Locker
	RLock()
	RUnlock()
}

type Setter

type Setter[T comparable] interface {
	Getter[T]
	// JawsSet may return ErrValueUnchanged to indicate value was already set.
	JawsSet(elem *jaws.Element, value T) (err error)
}

func MakeSetter

func MakeSetter[T comparable](v any) Setter[T]

func MakeSetterFloat64

func MakeSetterFloat64(v any) (s Setter[float64])

type SetterFloat64

type SetterFloat64[T numeric] interface {
	Getter[T]
	// JawsSet may return ErrValueUnchanged to indicate value was already set.
	JawsSet(elem *jaws.Element, value T) (err error)
}

Jump to

Keyboard shortcuts

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