0

ZK 6 Binding Problem

asked 2012-08-06 12:23:06 +0800

frieso gravatar image frieso
12

Hello,

I am not able to automatically update the data displayed in a grid when the model is changed by some thread in the application.


The ZUL file defines a grid which displays data from the model and a button that will refresh the data in the grid.



ZUL file:


<button label="Update Status" onClick="@global-command('update')" /> 

<grid model="@bind(vm.targetStatusList)">

...

</grid>





ViewModel file:
                ...

                @GlobalCommand 

                @NotifyChange("targetStatusList") 

                public void update(){

                               logger.debug( "Global Cmmmand 'update' called , event received. NotifyChange !");

                }

                ...



As the update() method in the ViewModel is annotated with @NotifyChange("targetStatusList") , the grid is refreshed every time

the button is pressed.





However, the ViewModel also registers an event listener which is called every time data in the Model changes by modifications

of a different thread:

    @Init
    public void init(){

                               

                               if (que == null) 

                                               que = EventQueues.lookup("statusUpdate", EventQueues.APPLICATION, true);

                               

                               que.subscribe(new EventListener() {

                                               public void onEvent(Event evt) {

                                                               logger.debug( "EventListener(): Event received, updating View ...");

                                                               update();

                                                               

                                               }

                               }); 

    }

 


The event handling method is actually called in this case when another thread modifies some data, and the onEvent() method then

calls the same mothod "update()" from above, but in this case, the data in the grid will NOT be refreshed and updated.



Does anybody have a clue why this is not working in the case the global command is called by the event callback method ?

Thanks in advance fo any help !

delete flag offensive retag edit

2 Replies

Sort by ยป oldest newest

answered 2012-08-07 01:35:00 +0800

henrichen gravatar image henrichen
3869 2
ZK Team

updated 2012-08-07 01:36:59 +0800

The annotation @NotifyChange is a *HINT* to the "ZK Binder". It tells the "ZK Binder" that it should update UI components that relative to the specified data whenever the annotated command is executed.

In case1, when you press the button, the onClick event sends a global command "update" to the "ZK Binder" to execute the update() command. So the "Binder" get the chance to read the "HINT" and update the grid. As specified in your zul page:

<button label="Update Status" onClick="@global-command('update')" ></button> 

In case2, update() method is called directly via another thread so the "ZK Binder" HAS NO IDEA what happened in this operation. Thus the @NotifyChange is "meaningless" in such situation.

To handle the issue, you have to send the global command to "ZK Binder" and let it do the update() command for you instead calling update() directly. ZK provide an utility method to do this. You should call

BindUtils.postGlobalCommand(null, null, "update", null)

in your thread.

http://www.zkoss.org/javadoc/latest/zk/org/zkoss/bind/BindUtils.html#postGlobalCommand(java.lang.String, java.lang.String, java.lang.String, java.util.Map)

link publish delete flag offensive edit

answered 2012-08-08 16:29:22 +0800

frieso gravatar image frieso
12

Henri,

thanks a lot for the helpful and quick response, it resolved the issue completely.

Frieso

link publish delete flag offensive edit
Your reply
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

RSS

Stats

Asked: 2012-08-06 12:23:06 +0800

Seen: 179 times

Last updated: Aug 08 '12

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