与父组件通信

Ele*_*ist 1 java tapestry

我有MyPage.tml页面和MyComponent.tml组件。

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd">
    <body>
        <t:mycomponent />
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

MyPage我需要根据 中发生的情况显示一些数据MyComponent。我怎样才能使一些数据MyComponent可用到MyPage?是否有类似“反向”参数(子级将参数传递给父级)之类的东西?

joo*_*ten 5

您的组件可以在页面中作为变量使用,您可以在页面中访问所需的变量,如下所示:

@Component(id = "myComponent")
private MyComponent myComponent;

@SetupRender //or any other render event method
private void setup() {
    Object compVariable = myComponent.getYourVariable();
}
Run Code Online (Sandbox Code Playgroud)

如果您问我,更优雅的是使用事件冒泡,因为它可以更轻松地将某些逻辑重构为更深的组件(如果需要)。

成分:

@Inject
private ComponentResources resources;

@SetupRender //or any other lifecycle event method
private void triggerEvent() {
    Object yourVariable = new Object();
    resources.triggerEvent("YOUR_EVENT_NAME", new Object[]{yourVariable}, null);
    //add an event callback if needed where I use null here
}
Run Code Online (Sandbox Code Playgroud)

页:

@OnEvent(value = "YOUR_EVENT_NAME")
private void handleComponentEvent(Object yourVariable) {
    //do something with yourVariable
    //even return something which would then can be handled by your component callback handler
}
Run Code Online (Sandbox Code Playgroud)