Unable to programmatically retrieve a regular macro component definition

cvarona
554 1 6

Hi,

I'm trying to follow the steps in the development guide in order to programmatically instantiate a regular macro component. However it's beeing impossible for me to retrieve the macro component definition. I've tried the following in the online demo:

<?component name="newProduct" macroURI="/userguide/misc/includedMacro.zul"?>
<zk>
<hbox>
<newProduct />
<newProduct title="Monitor" img="/img/item1.jpg" desc="The new HDMI monitor"/>
<newProduct id='_p' title="Cell" img="/img/item2.jpg" desc="This is zPhone"/>
<button label='Try and fectch "newProduct" component definition' onClick='alert( "" + page.getComponentDefinition("newProduct", false ) + " in page " + page );' />
<button label='What page do the newProduct instances belong to?' onClick='alert( _p.getDefinition().getName() + " in page " + _p.getPage() );' />
</hbox>
</zk>
The newProduct instances actually belong to the current page, but it looks like the page is unaware of their component definitions. Must it actually be like that? Am I missing something? Any help would be much appreciated.

With kind regards

César Varona

delete retag edit

3 Replies

PeterKuo
481 2

I think it's problem of online demo. Maybe it's security reason.
I tried it in local machine, it runs well.
[ComponentDefinition: newProduct] in page [Page z_94_0] "

cvarona
554 1 6

Hi Peter,

I tried it on the demo because I was being unable to get the macro component definition and wanted to put up a simple example. Since it failed the same way it did in my environment I supposed it was actually like that.

After reading your post I tried a simplified version and discovered you're right. After reflecting for a while about it I realised it was my loading the page with the <?component ?> directive via Executions.createComponents that made the difference. In other words, this works:

macro.zul

<label value='This is just a test' />

test.zul

<?xml version='1.0' encoding='UTF-8' ?>
<?component name="macro" macroURI="/macro.zul"?>
<window id='w'>
<zscript>
HtmlMacroComponent macro = ( HtmlMacroComponent ) page.getComponentDefinition( "macro", false ).newInstance( page, null );
macro.setParent( w );
macro.applyProperties();
macro.afterCompose();
</zscript>
</window>

This however doesn't:

invoke_test.zul

<window title='Invokes test' id='itest'>
<zscript>Executions.createComponents( "/test.zul", itest, null );</zscript>
</window>

Do you think this is so for some good reason or it can be considered rather a bug?

With kind regards

César Varona

cvarona
554 1 6

Curiously enough, if I modify test.zul like this

<?xml version='1.0' encoding='UTF-8' ?>
<?component name="macro" macroURI="/macro.zul"?>
<window id='w'>
<macro id='_justForTheSakeOfInstantiation' visible='false' />
<zscript>
HtmlMacroComponent macro = ( HtmlMacroComponent ) _justForTheSakeOfInstantiation.getDefinition().newInstance( page, null );
macro.setParent( w );
macro.applyProperties();
macro.afterCompose();
</zscript>
</window>
it works.

[hide preview]