function

NewAdminLoader

Define a Go read function whose result is supplied to an admin component.

Example

go
type CountInput struct{}
type CountData struct {
  Total int `json:"total"`
}

var PostCount = ridu.NewAdminLoader("post-count",
  func(ctx ridu.AdminLoadContext, _ CountInput) (CountData, error) {
    posts, err := ctx.List("posts", ridu.ListOptions{Limit: 1})
    return CountData{Total: posts.Total}, err
  },
)

Arguments

ArgumentTypeDescription
keystringApplication-wide unique key, at most 80 characters. Start with a lowercase letter; use lowercase letters, digits, and single hyphens between words, such as editorial-post-summary. Use the same key from generated adminLoaders.
handlerfunc(AdminLoadContext, Input) (Output, error)Read function receiving the current request context and URL query inputs. Return browser-safe data or an error.

Returns

A loader definition to append to Config.Admin.Loaders. Defining or registering it does not run the data read.

AdminLoaderDefinition

How it works

Register the result in Config.Admin.Loaders, then run ridu generate. Generation derives the input and output TypeScript types from your Go structs.

Use explicit json tags. Query inputs are scalar fields; missing values retain Go zero values. Return structs, scalar values, arrays, slices, pointers, or time.Time; maps, recursive types, and custom JSON codecs are not supported.

The function can run on page entry, URL changes, or refresh. Keep it read-only. ctx.List, ctx.Find, and ctx.Global enforce Ridu permissions and the current locale; external services need your own access checks.

Full signature

func NewAdminLoader[Input, Output any]( key string, handler func(AdminLoadContext, Input) (Output, error), ) AdminLoaderDefinition

Source core/admin_loader.go:95

Related types

Related guides