Data sharing
What is data sharing ?
Data sharing is a mechanism that allows you to share a data source between multiple UI views.
This offers several advantages :
- changes made to the data by one UI view is automatically propagated to all the UI views it shares its data with
- only one request to fetch data is made by the main UI view instead of one request by UI view
Using data sharing
In a UI template
The <ui-view> component provides argument data-shared-with for sharing data.
When navigating
When navigating by code, the NavigationOptions provides the WithDataSharing method for sharing data.
When to use data sharing ?
When dealing with a complex UI view, it can be useful to cut it in several parts to be able partition it by theme and be able to reuse the smaller UI views in other parts of the application.
For example, if we have a party edit UI view that contains a big template, we might want to isolate the customer account information in a separate UI view and reuse it in a invoice UI.
<ui-view name="CustomerAccountUI" data-shared-with="#Main" />
You can also use data sharing when opening a new screen / modal popup by navigating. Doing so, the opened UI view will not load its data and use the shared data as its data source.
This can be useful to isolate the content of a modal popup.
NavigationOptions options = new NavigationOptions(UIViews.NewCustomerAccountUI)
.OnTarget(NavigationTarget.Popup)
.WithDataSharing(Datasource, Position);
await NavigateAsync(options);
Constraints
Because a UI view opened with data sharing gets its data from a parent UI view, they both must use a compatible structure.
Consider a UI view A that shares its data with a UI view B :
- UI view
Amust be based on an entity view similar to the entity view UI viewBis based.
This means that while entity viewAand entity viewBcan be different, all the bound properties of entity viewBmust exist on entity viewA. - UI view
Amust contain subviews that are similar to the ones existing on UI viewB.
Consider a subview nameSVexisting on UI viewB.
This means that :- a subview with the same name must exist on UI view
A - the subview on UI view
Acan use a different UI view than the one on UI viewB - the subview on UI view
Amust use a UI view based on the same entity view as the one on UI viewB
- a subview with the same name must exist on UI view
Identifying data sharing with incoherent UI views
Neos cannot detect when generating that the above constraints are not respected because data sharing can be used when navigating by code.
However you will be notified when opening a screen using an incoherent data sharing configuration :
- in development, an error popup listing the problems is displayed and an error is logged in the browser console
- in production, an error is logged in the browser console
In both cases, you will be able to see these errors in Application Insights if it is configured for the cluster.