0

我想要做的是创建一个自定义控件,它自己确定是否应该加载它。所以我查看了自定义控件的加载属性,但它没有按我预期的方式工作。它至少似乎忽略了这个属性。我整理了一个小测试控件来演示。

<?xml version="1.0" encoding="UTF-8"?>
 <xp:view xmlns:xp="http://www.ibm.com/xsp/core" pageTitle="Test" loaded="false">
   This is a test custom control.
 </xp:view>

控件始终显示,即使它的加载属性设置为 false。Nw 我能做的是将自定义控件放在我的页面上并在那里设置加载的属性。这很好用,但我想避免每次使用控件时都必须设置该属性。我想将代码放在控件中,看看是否应该加载它。

我错过了什么吗?

是的,我知道我可以将所有内容放在控件上的 div 中而不加载 div,但我宁愿根本不加载控件,如果不应该加载它。

4

1 回答 1

1

I'm not entirely sure why the loaded attribute is even available on the root element, because what it does is inherently impossible to evaluate at that level.

As opposed to rendered, which determines whether any representation of the component will be sent to the consumer, loaded determines whether or not the component is even created. So if rendered is false, it just doesn't display... if loaded is false, it doesn't even exist. The component needs to exist for the view tag attributes to be evaluated, so it's too late by then to stop it from existing since it needs to exist in order to be told not to exist.

As a result, in order to use loaded to suppress the entire control, the attribute needs to be set on the control reference. In other words, when adding the control to an XPage (or another custom control), set a loaded expression on the tag for the control. If it evaluates to false, then the control will never be loaded.

Remember, all of this ends up executing as Java... if you block construction of a class instance, you don't have a class instance to ask whether it should have been constructed. Conversely, if you create a class instance, then ask it whether it should exist... too late, it already exists. If the custom control should own the logic for whether its contents should exist, then specifying that logic in the loaded attribute of a single child container is the "right" approach.

于 2013-03-18T21:20:22.077 回答