欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

use【组件公共属性】

程序员文章站 2022-07-13 21:47:18
...

语法

apply="a-class-name
"
apply="class1
, class2
,..."
apply="${EL_returns_a_class_or_a_collection_of_classes
}"
apply="${EL_returns_an_instance_or_a_collection_of_Composer_instances
}"

可以指定一个类、类的集合,apply用于初始化组件 (译者注:详见 use和apply的区别 ) .该类必须实现 Composer 接口 ,然后可以在doAfterCompose 方法内做一些初始化工作, 因为该方法会在组件和其子组件初始化后自动调用(译者注:callback)

1
< window apply = "foo.MyComposer" />

此外,也可以指定一个 Composer 类型对象实例,或者使用el表达得到的Composer对象实例的集合.

注意:  如果指定了el表达式,那么表达式会在组件初始化之前执行. 因此你不能访问组件的引用, 内置隐含变量self  引用的是父组件,如果有的话,或者是当前page,如果当前组件是根组件。

 

===============================

译者注:

我设计了一个错误验证上述观点,因为隐含对象self标示的组件对应的类中,一定没有name123456的getter报错,

但异常信息一定会标示name123456属性所属的类,window组件对应org.zkoss.zul.Window类

label对应org.zkoss.zul.Label类,那么在label appy属性中的self到底是标示那个组件呢?

<window title="My First Window" border="normal" width="200px">

<label apply="${self.name123456}"/>

</window>

运行结果报错信息如下:

Unable to find a value for "name123456" in object of class "org.zkoss.zul.Window" using operator "."

因此apply中的self标示的是父组件或者当前页面对象


======================================================================

 

如果想获取更多的控制权,例如异常处理,可以实现 ComposerExt  接口

假如有一个composer,你想引用到没一个页面,但又不想在每个页面都重复指定,可以注册一个系统级的composer。了解更多信息见 ZK Developer's Reference: System-level Composers .



      原文:http://books.zkoss.org/wiki/ZUML%20Reference/ZUML/Attributes/apply

Syntax

apply="a-class-name
"
apply="class1
, class2
,..."
apply="${EL_returns_a_class_or_a_collection_of_classes
}"
apply="${EL_returns_an_instance_or_a_collection_of_Composer_instances
}"

It specifies a class, a collection of classes that are used to initialize the component. The class must implement the Composer  interface. And then, you can do the initialization in thedoAfterCompose  method, since it is called after the component and all its children are instantiated.

1
< window apply = "foo.MyComposer" />

In addition, you specify a Composer  instance, or a collection of Composer  instances by use of EL expressions.

Note:  the EL expressions are, if specified, evaluated before the component is instantiated. So you cannot reference to the component. Moreover, the self  variable references to the parent component, if any, or the current page, if it is the root component, in the EL expressions specified in this attribute.

If you want more control such as handling the exception, you can also implement the ComposerExt  interface.

If you have a composer that you'd like to apply to every page, you don't need to specify it in every page. Rather, you could register a system-level composer. For more information, please refer to ZK Developer's Reference: System-level Composers .

相关标签: zk