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
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.
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
errorproperty 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
exactisTrue, then the path and deps must match exactly. By default this isFalse. IfFalsethen 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_changedandhash_changedevents. This method is useful during instantiation of the form. First set up your event handlers, then callraise_init_events().