与父组件通信

问题描述 投票:1回答:3

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

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd">
    <body>
        <t:mycomponent />
    </body>
</html>

我需要根据MyPage发生的事情在MyComponent上显示一些数据。我怎样才能将MyComponent的一些数据提供给MyPage?是否有类似“反向”参数(将子参数传递给父级)?

java tapestry
3个回答
4
投票

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

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

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

如果你问我更优雅的是使用事件冒泡,因为如果需要的话,它可以更容易地将一些逻辑重构为更深层的组件。

零件:

@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
}

页:

@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
}

1
投票

您可以使用通常的tapestry参数。

<t:mycomponent value="myValue"/>

如果该值将在组件侧更改,则它将在容器侧可用,反之亦然。


0
投票

我根据具体情况使用了所有这三种方法。我通常更喜欢事件冒泡,这是有道理的。

© www.soinside.com 2019 - 2024. All rights reserved.