0

Using @Wire in macro component based on Window

asked 2014-07-30 21:53:58 +0800

snaef999 gravatar image snaef999
6 1

I am trying to create a macro component with Window as the root component.

clientSelector.zul:

<?xml version="1.0" encoding="UTF-8"?>
<window visible="true" width="120px" border="normal" mode="overlapped"
    apply="org.zkoss.bind.BindComposer"
    viewModel="@id('clientSelectorVM') @init('com.snaef.zk.widget.ClientSelector')">
    <textbox id="mytextbox" value="@bind(clientSelectorVM.textValue)" />
    <button onClick="@global-command('clientSelectorSave', 
    returnValue=clientSelectorVM.textValue)" />
</window>

ClientSelectorVM.java

public class ClientSelectorVM extends HtmlMacroComponent {

    private static final long serialVersionUID = 1L;
    private String _textValue;

    @Wire
    Textbox mytextbox;

    public String getTextValue() {
        return _textValue;
    }


    public void setTextValue(String textValue) {
        _textValue = textValue;
        mytextbox.setValue(_textValue);
    }
}

However, when I try to use the component I get null exception on the line

mytextbox.setValue(_textValue);

I tried several things including specifying the id name directly @Wire("@mytextbox") and even tried bypassing the annotation and using the getfellow() method to directly get the component but both still return a null pointer exception.

If I simply change my outer component to a Div for example it will work fine.

From the documentation I see that Window uses its own ID space and I suspect the problem relates to this somehow but I dont know how or why.

How do I access mytextbox ?

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2014-07-31 02:17:59 +0800

hawk gravatar image hawk
3250 1 5
http://hawkphoenix.blogsp... ZK Team

ViewModel will not automatically wire a zk component. Normally, if you develop an application under MVVM pattern, I don't suggest you to wire a component. It breaks the MVVM pattern.

If you want to wire a component, you have to call Selectors to wire for you.

@AfterCompose
public void afterCompose(@ContextParam(ContextType.VIEW) Component view){
    Selectors.wireComponents(view, this, false);
}

please refer to http://books.zkoss.org/wiki/ZK%20Developer's%20Reference/MVVM/Advanced/Wire%20Components

link publish delete flag offensive edit
0

answered 2014-07-31 04:17:44 +0800

snaef999 gravatar image snaef999
6 1

I agree with sticking to the MVVM pattern. I think my quest came about when the binding annotation in my zul file did not work.

In my example above my textbox should automatically update when I change the value of _textValue because I bind to it in zul, but it does not. Can you help me understand what is going wrong? I monitor the call to setTextValue() and I see it updating the value in the VM but the binding does not seem to work.

link publish delete flag offensive edit

Comments

Is it really a marco component what you are trying to achieve or just some page what you want to include somewhere. Did you do a notify that you have changed textValue to the binder? if not => add a notifychange with annotations if the method is a setter or command, ortherwise with BindUtils.

chillworld ( 2014-07-31 11:07:21 +0800 )edit

According your code above, your binding VM doesn't match your viewmodel class name:

binding VM: @init('com.snaef.zk.widget.ClientSelector')"

ViewModel class: public class ClientSelectorVM...

hawk ( 2014-08-04 04:46:44 +0800 )edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
2 followers

RSS

Stats

Asked: 2014-07-30 21:53:58 +0800

Seen: 69 times

Last updated: Jul 31 '14

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More