DataLoadCoordinator
DataLoadCoordinator facet 支持触发数据加载,以及声明式的将数据加载器和数据容器、可视化组件、界面事件进行连接。
组件的 XML 名称:dataLoadCoordinator。
基本用法
如需在界面的 BeforeShowEvent 或 fragment 的 AttachEvent 事件触发所有的数据加载器,只需在界面的 XML 描述中添加 DataLoadCoordinator,其属性 auto="true":
<facets>
    <dataLoadCoordinator auto="true"/>
</facets>工作模式
可以配置 DataLoadCoordinator 在自动、手动或半自动模式下工作。
自动模式
此模式中,DataLoadCoordinator 依赖使用特定前缀的参数名称。前缀表示提供参数和事件的组件。
默认情况下,数据容器的参数前缀是 container_,可视化组件的参数前缀是 component_。通过 componentPrefix 和 containerPrefix 属性可以使用不同的前缀。
使用自动模式时,如果加载器的查询语句中没有参数(静态查询语句不包含 查询条件),则该加载器会在 Screen 的 BeforeShowEvent 或 ScreenFragment 的 AttachEvent 中自动刷新。
| 如果主查询语句中有自定义参数(非  如需刷新被忽略的加载器,请通过编码的方式设置参数值并调用加载器的  | 
示例:
<window caption="msg://automaticModeScreen.caption"
        xmlns="http://jmix.io/schema/ui/window"
        xmlns:c="http://jmix.io/schema/ui/jpql-condition"> (1)
    <data readOnly="true">
        <collection id="citiesDc"
                    class="ui.ex1.entity.City">
            <fetchPlan extends="_base"/>
            <loader id="citiesDl">
                <query>
                    <![CDATA[select e from uiex1_City e]]> (2)
                    <condition>
                        <and>
                            <c:jpql>
                                <c:where>e.name like :component_nameField</c:where> (3)
                            </c:jpql>
                        </and>
                    </condition>
                </query>
            </loader>
        </collection>
        <collection id="customersDc"
                    class="ui.ex1.entity.Customer">
            <fetchPlan extends="_base"/>
            <loader id="customersDl" >
                <query>
                    <![CDATA[
                        select e from uiex1_Customer e
                            where e.city = :container_citiesDc
                    ]]> (4)
                </query>
            </loader>
        </collection>
    </data>
    <facets>
        <dataLoadCoordinator auto="true"/>
    </facets>
    <layout>
        <textField id="nameField"/>
    </layout>
</window>| 1 | 添加支持 JPQL 条件的 schema。 | 
| 2 | 查询中没有参数,所以 citiesDl加载器会在BeforeShowEvent触发。 | 
| 3 | citiesDl加载器也会在nameField组件值更改的时候触发。由于条件使用了like子句,组件值会被自动包装在'(?i)% %'中,提供大小写不敏感搜索。 | 
| 4 | customersDl加载器会在citiesDc数据容器内容变化时触发。 | 
手动模式
此模式中,内部的 refresh 元素定义数据加载器何时触发刷新。
示例:
<window caption="msg://manualModeScreen.caption"
        xmlns="http://jmix.io/schema/ui/window"
        xmlns:c="http://jmix.io/schema/ui/jpql-condition"> (1)
    <data readOnly="true">
        <collection id="citiesDc"
                    class="ui.ex1.entity.City">
            <fetchPlan extends="_base"/>
            <loader id="citiesDl">
                <query>
                    <![CDATA[select e from uiex1_City e]]>
                    <condition>
                        <and>
                            <c:jpql>
                                <c:where>e.name like :name</c:where>
                            </c:jpql>
                        </and>
                    </condition>
                </query>
            </loader>
        </collection>
        <collection id="customersDc"
                    class="ui.ex1.entity.Customer">
            <fetchPlan extends="_base"/>
            <loader id="customersDl" >
                <query>
                    <![CDATA[
                        select e from uiex1_Customer e
                            where e.city = :city
                    ]]>
                </query>
            </loader>
        </collection>
    </data>
    <facets>
        <dataLoadCoordinator>
            <refresh loader="citiesDl">
                <onScreenEvent type="Init"/> (2)
                <onComponentValueChanged component="nameField"
                                         likeClause="CASE_INSENSITIVE"
                                         param="name"/> (3)
            </refresh>
            <refresh loader="customersDl">
                <onContainerItemChanged container="citiesDc"
                                        param="city"/> (4)
            </refresh>
        </dataLoadCoordinator>
    </facets>
    <layout>
        <textField id="nameField"/>
    </layout>
</window>| 1 | 添加支持 JPQL 条件的 schema。 | 
| 2 | citiesDl加载器会在InitEvent事件触发。 | 
| 3 | citiesDl加载器会在nameField组件值更改的时候触发。由于条件likeClause属性,组件值会被自动包装在'(?i)% %'中,提供大小写不敏感搜索。 | 
| 4 | customersDl加载器会在citiesDc数据容器内容变化时触发。 | 
半自动模式
当 auto 属性设置为 true 并且也有一些手动配置的触发器,DataLoadCoordinator 会为所有没有手动配置的加载器做自动配置。
示例:
<window caption="msg://semiAutomaticModeScreen.caption"
        xmlns="http://jmix.io/schema/ui/window">
    <data readOnly="true">
        <collection id="brandsDc"
                    class="ui.ex1.entity.Brand">
            <fetchPlan extends="_base"/>
            <loader id="brandsDl">
                <query>
                    <![CDATA[select e from uiex1_Brand e]]>
                </query>
            </loader>
        </collection>
        <collection id="customersDc"
                    class="ui.ex1.entity.Customer">
            <fetchPlan extends="_base"/>
            <loader id="customersDl" >
                <query>
                    <![CDATA[
                        select e from uiex1_Customer e
                            where e.favouriteBrands = :container_brandsDc
                    ]]> (1)
                </query>
            </loader>
        </collection>
    </data>
    <facets>
        <dataLoadCoordinator auto="true">
            <refresh loader="brandsDl">
                <onScreenEvent type="AfterShow"/> (2)
            </refresh>
        </dataLoadCoordinator>
    </facets>
    <layout>
    </layout>
</window>| 1 | customersDl加载器配置在brandsDc数据容器内容变化时自动触发。 | 
| 2 | brandsDl为手动配置,在AfterShowEvent事件触发。 | 
refresh 元素
refresh 元素支持为数据加载器定义刷新的条件。
该元素的唯一属性是 loader,定义加载器的 id。
refresh 元素可以有下列内部的元素,用于定义触发条件:
- 
onComponentValueChanged- 当可视化组件的值改变时触发加载器。有下列属性:- 
component- 指定可视化组件的id。
 - 
likeClause- 如果在查询条件中使用like表达式,可以定义下面三种搜索模式之一:- 
NONE- 默认值。
- 
CASE_SENSITIVE- 大小写敏感。
- 
CASE_INSENSITIVE- 大小写不敏感。
 
- 
 - 
param- 指定查询参数名称。
 <dataLoadCoordinator> <refresh loader="citiesDl"> <onComponentValueChanged component="nameField" likeClause="CASE_SENSITIVE" param="name"/> </refresh> </dataLoadCoordinator>
- 
- 
onContainerItemChanged- 当数据容器中的内容改变时触发加载器。有下列属性:- 
container- 指定数据容器的id。
 - 
param- 指定查询参数名称。
 <dataLoadCoordinator> <refresh loader="citiesDl"> <onContainerItemChanged container="citiesDc" param="name"/> </refresh> </dataLoadCoordinator>
- 
- 
onScreenEvent- 在界面事件中触发加载器。有下列属性:- 
type- 定义界面事件的类型,可能值:- 
Init- 在 InitEvent 事件中触发。
- 
AfterInit- 在 AfterInitEvent 事件中触发。
- 
BeforeShow- 在 BeforeShowEvent 事件中触发。
- 
AfterShow- 在 AfterShowEvent 事件中触发。
 
- 
 <dataLoadCoordinator> <refresh loader="citiesDl"> <onScreenEvent type="BeforeShow"/> </refresh> </dataLoadCoordinator>onScreenEvent会在界面控制器中使用@Subscribe注解定义的对应消息监听器 之后 触发。
- 
- 
onFragmentEvent- 在 fragment 事件中触发加载器。有下列属性:- 
type- 定义界面 fragment 事件的类型,可能值:- 
Init- 在 InitEvent 事件中触发。
- 
AfterInit- 在 AfterInitEvent 事件中触发。
- 
Attach- 在 AttachEvent 事件中触发。
 
- 
 <dataLoadCoordinator> <refresh loader="citiesDl"> <onFragmentEvent type="Init"/> </refresh> </dataLoadCoordinator>
- 
XML 属性
| 可以在 Studio 界面设计器中的 Jmix UI 组件面板查看和编辑 facet 支持的属性。 | 
auto - componentPrefix - containerPrefix - id