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 Index(IndexTemplate):
def __init__(self, routing_context: RoutingContext, **properties):
self.routing_context = routing_context
super().__init__(**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
dictreturned by thecache_depsmethod. nav_context- The navigation context for the current route. This is a
dictand can be set by passing anav_contextargument to thenavigatemethod. (Or equivalently by setting thenav_contextattribute on aNavLink/Anchorcomponent). form_properties- The form properties for the current route. This is a
dictand can be set by passing aform_propertiesargument to thenavigatemethod. (Or equivalently by setting theform_propertiesattribute on theNavLink/Anchorcomponent). Note theform_propertiesare 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_datamethod. revalidatingTruewhen routing is currently refreshing cached data in the background.
Events¶
The RoutingContext instance will emit events when the route changes.
data_loading- Emitted when the data is loading. This also fires from
raise_init_events()if the current context already has cached data and is revalidating it in the background. data_loaded- Emitted when data loads successfully.
raise_init_events()also emits this event with the current data and error, so checkrouting_context.errorwhen handling initial state. 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
exactisTrue, then the path and deps must match exactly. By default this isFalse. IfFalse, matching paths and their descendants are invalidated when their dependencies contain all the entries in this context'sdeps. refetch(*, silent=None)- Invalidates cached forms and data for this routing context with
exact=True. If this is the current routing context, starts a new data load and returns its asynchronous result. Otherwise, returns without loading data. Thesilentvalue is passed through toload_data(...)as a loader argument. Forserver_fnroutes,silent=Trueusesanvil.server.call_s(...),silent=Falseusesanvil.server.call(...), andsilent=Noneuses the route default. Customload_data(...)implementations can inspectsilentand choose how to handle loading indicators or silent/background fetches. raise_init_events()- Raises
data_loaded,query_changedandhash_changedfor the current state. Also raisesdata_errorif an error exists anddata_loadingif data is revalidating. This method is useful during instantiation of the form. First set up your event handlers, then callraise_init_events().