Use a Resource provider to define the structural entities of your application and how they map to browser URLs. This central definition allows the framework to automatically handle routing and context resolution.
A resource is defined by a name and a set of action patterns:
interface ResourceDefinition {
name: string
// Action patterns (can be a string path or a custom parse function)
list?: string | ResourceActionParse
create?: string | ResourceActionParse
edit?: string | ResourceActionParse
show?: string | ResourceActionParse
meta?: {
parent?: string
hide?: boolean
[key: string]: any
}
}
Each resource action is associated with a URL pattern. These patterns follow the regexparam syntax, supporting dynamic segments like :id.
meta.parent property, which helps in breadcrumb generation and navigation menu grouping.Common patterns include:
/posts/posts/new/posts/:id/edit/posts/:idWhen the browser URL matches one of these patterns, the framework identifies the corresponding resource and action.
The framework determines the current resource context by resolving the browser's location against your resource definitions.
path, query, and params.path against all defined resource patterns. When a match is found, the framework extracts the resource, the action, and any parameters like id.Use useResource to access the currently resolved resource context in your components. This composable reactively tracks the browser location and provides you with the matched resource definition, the current action, and the record ID if applicable.
<script setup lang="ts">
import { useResource } from '@ginjou/vue'
const resolved = useResource()
// resolved.value will contain:
// - resource: The matched ResourceDefinition
// - action: 'list' | 'create' | 'edit' | 'show'
// - id: The record ID (for 'edit' or 'show' actions)
</script>
<template>
<div>
<h1>Current Resource: {{ resolved?.resource.name }}</h1>
<p>Action: {{ resolved?.action }}</p>
<p v-if="resolved?.id">
ID: {{ resolved.id }}
</p>
</div>
</template>
useLocation to track the current path and resolveResource to match it against the provided definitions.