1

can't postNotifyChange in event listener

asked 2018-11-26 13:24:51 +0800

superium gravatar image superium
17 2

In my VM, i'me listening an event queue and trying to put an notice to the user. but postNotifyChange fail. i had to handle component to make the client be notified. is there a better way to do it in VewMmodel ?

@Init
    public void init() {

            EventQueue<Event> eq = EventQueues.lookup("postNotice", EventQueues.APPLICATION, true);
            System.out.println("subscribing event.");
            eq.subscribe(new EventListener<Event>() {

                @Override
                public void onEvent(Event event) throws Exception {
                    System.out.println("received event in homeVM");


                    msg = .....
                    BindUtils.postNotifyChange(null, null, this, "msg");                // msg is always refreshed, even without this.
                    BindUtils.postNotifyChange(null, null, this, "msgvisible");         // this never notice the client.

                }
            }
    }
delete flag offensive retag edit

5 Answers

Sort by ยป oldest newest most voted
0

answered 2018-12-03 18:28:41 +0800

chillworld gravatar image chillworld flag of Belgium
5322 4 9
https://github.com/chillw...

Okay,

Now we are getting there.
the information from another session is of course very crucial.
Please look at the following small talk.

https://www.zkoss.org/wiki/SmallTalks/2014/March/ChatwithGlobalCommand

It will explain you on how to achieve the same, without any personal use of the eventQueue (the global commad will work with eventqueue but that is abstracted out)

The example calls first a command in order to not explose it t all the other sessions, the gobal command is then exposing to all other sessions.

Chill.

link publish delete flag offensive edit
0

answered 2018-11-27 16:22:37 +0800

chillworld gravatar image chillworld flag of Belgium
5322 4 9
https://github.com/chillw...

I would start with mixing MVC and MVVM is never good and you could use Command and GlobalCommand as a better way to use the Queue's in MVVM.

But I also notice the keyword this.
Where do you think that will be targeted? It's your eventlistener, not you Viewmodel!
To pass the viewmodel instance ClassName.this.

Chill.

link publish delete flag offensive edit
0

answered 2018-11-30 13:46:58 +0800

superium gravatar image superium
17 2

Thanks for you answer. i used ClassName.class before but not work ,ater that i changed it to "this",still not work.

use command do you mean like this :

@Init
public void init() {

    EventQueue<Event> eq = EventQueues.lookup("postNotice", EventQueues.APPLICATION, true);
    System.out.println("subscribing event.");
    eq.subscribe(new EventListener<Event>() {

        @Override
        public void onEvent(Event event) throws Exception {
            doRefreshMsg(event.getDate());

        }
    }
}


@Command
@NotifyChange ("*")
public void doRefreshMsg(....){
    //do something here.
}
link publish delete flag offensive edit

Comments

Indeed, that would ben a better options. You don't need to sobscribe the queue in the init, just target the event in zul to that command

chillworld ( 2018-12-01 00:33:03 +0800 )edit

Otherwise i need more code so i can help refactor it

chillworld ( 2018-12-01 00:34:30 +0800 )edit
0

answered 2018-12-03 17:46:17 +0800

superium gravatar image superium
17 2

thanks for your suggest. i tried to invoke the command in the eventlistener, the command executed successfully, but the @NotifyChange not work in this situation.

the second way, i've tried too. but failed to receive the event. my event is sent from another session (by another user) and this event couldn't be received by a component in another session. i did like this :

// when a user post an article in post.zul:

    eq.publish(new Event("onInvitePost", Path.getComponent("//main/home"), myMsgBean));

in another page:

//notify another user who was invited :
in home.zul:

    <?page title="main" id="main" contentType="text/html;charset=UTF-8"?>

        <borderlayout id="home"
            viewModel="...."        
            onInvitePost="@command('doInform',e=event)">
    ...

and in VM:

@Command
@NotifyChange({ "msg", "msgvisible" })
public void doInform(@BindingParam("e") Event e) {
    System.out.println(" onIvitePost received in notice...");
    Clients.showNotification("doInforming");
}

as a result, the command doInform never execute. event of Desktop Scope could be send to a special target ,but this case is a Application Scope event queue. sames it has to define an event queque in somewhere and sign an event listener in the event queque.

link publish delete flag offensive edit
0

answered 2018-12-04 15:14:23 +0800

superium gravatar image superium
17 2

Thank you very much! It works perfectly! Great global command! just a postGlobalCommand solves all problem. there's no need to sign an eventQueue manually any more.

Further more, does postGlobalCommand need to be in an Execution ? if the command was launched by some other event instead of another session(eg.sth changes in database, or Pay Notice from bank) .

if it works, seems that there's no need to sign an event queue manually any more ,do you agree ?

link publish delete flag offensive edit

Comments

In MVVM there is indeed very little need of manual triggering the eventqueue. Almost everything can be done with command and globalcommand. Just going from 1 user session to another user session is some specific knowledge.

chillworld ( 2018-12-04 17:13:03 +0800 )edit

Thank you very much! you're so kind!

superium ( 2018-12-05 10:40:11 +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

RSS

Stats

Asked: 2018-11-26 13:24:51 +0800

Seen: 13 times

Last updated: Dec 04

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