Routing Context¶
A RoutingContext
instance is passed to a form when it is instantiated.
It provides information about the current route and the current navigation context.
from ._anvil_designer import IndexTemplate
from routing.router import RoutingContext
class IndexTemplate(IndexTemplate):
def __init__(self, routing_context: RoutingContext, **properties):
self.routing_context = routing_context
self.init_components(**properties)
Autocompletion
Adding the RoutingContext
type definition will allow Anvil to show autocompletion for the routing_context
property.
Properties¶
path
- The path for the current route.
params
- The parameters for the current route.
query
- The query parameters for the current route.
hash
- The hash for the current route.
deps
- The dependencies
dict
returned by thecache_deps
method. nav_context
- The navigation context for the current route. This is a
dict
and can be set by passing anav_context
argument to thenavigate
method. (Or equivalently by setting thenav_context
attribute on aNavLink
/Anchor
component). form_properties
- The form properties for the current route. This is a
dict
and can be set by passing aform_properties
argument to thenavigate
method. (Or equivalently by setting theform_properties
attribute on theNavLink
/Anchor
component). Note theform_properties
are passed as keyword arguments when instantiating a form. For more details see the Navigation section. error
- The error that occurred when loading the form or loading the data. This is particularly useful when displaying error messages in your error form.
data
- The data for the current route. This is the value returned from the
load_data
method.
Events¶
The RoutingContext
instance will emit events when the route changes.
data_loading
- Emitted when the data is loading.
data_loaded
- Emitted when the data has been loaded, or when the data has an error. To determine if the data is loaded successfully, check the
error
property isNone
. data_error
- Emitted when the data has an error.
query_changed
- Emitted when the query parameters change.
hash_changed
- Emitted when the hash changes.
Methods¶
invalidate(exact=False)
- Invalidates any cached data or forms for this routing context. If
exact
isTrue
, then the path and deps must match exactly. By default this isFalse
. IfFalse
then any path or deps that are a subset of path and deps arguments will be invalidated. refetch()
- Invalidates the data for this routing context (with exact=True) and then loads the data again.
raise_init_events()
- Raises the
data_loaded
,data_loading
,data_error
,query_changed
andhash_changed
events. This method is useful during instantiation of the form. First set up your event handlers, then callraise_init_events()
.