1

How to notify change from Local class (Listener) [closed]

asked 2018-01-02 14:27:30 +0800

adilov gravatar image adilov
65 4

updated 2018-01-02 14:33:17 +0800

@Command
public void someMethod() {
  final Object bean = this;
  EventListener<ClickEvent> clickListener = new EventListener<Messagebox.ClickEvent>() {
    public void onEvent(ClickEvent event) throws Exception {
      if(Messagebox.Button.YES.equals(event.getButton())) {
        // ...
        BindUtils.postNotifyChange(null, null, bean, "yourIDhere");
      }
    }
};

Messagebox.show("Question?", "Title", new Messagebox.Button[]{Messagebox.Button.YES, Messagebox.Button.NO }, Messagebox.QUESTION, clickListener);
}

Just wanted to share a "trick" that I had to use... If you have local class, e.g. listener as in the code above, and you want to notify for a change, the @NotifyChange annotation will not help you. Or it just didn't work in my case - it doesn't matter if it's above the "delete()" method or above the "onEvent". In this case the "BindUtils.postNotifyChange" can be used, BUT the bean reference must be passed as a parameter, initialized outside the anonymous class.

delete flag offensive retag edit

The question has been closed for the following reason "the question is answered, right answer was accepted" by cor3000
close date 2018-01-04 14:27:55

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-01-03 15:26:20 +0800

cor3000 gravatar image cor3000
6280 2 7

Hi Adilov,

thanks for sharing your 'trick'. In fact it's not a trick but rather the programmatic way to notify change a property. You are right, @NotifyChange can't help you if you call a method yourself, and that's why you correctly found the BindUtils.postNotifyChange() method. When using a local inner class you can reference the outer this like in the example below. No need for an additional variable.

public class SomeViewModel {
  @Command
  public void someMethod() {
    EventListener<ClickEvent> clickListener = new EventListener<Messagebox.ClickEvent>() {
      public void onEvent(ClickEvent event) throws Exception {
        if(Messagebox.Button.YES.equals(event.getButton())) {
          // ...
          BindUtils.postNotifyChange(null, null, SomeViewModel.this, "yourIDhere");
        }
      }
  };

  Messagebox.show("Question?", "Title", new Messagebox.Button[]{Messagebox.Button.YES, Messagebox.Button.NO }, 
    Messagebox.QUESTION, clickListener);
  }

}

When using Java 8 this isn't even necessary anymore:

@Command
public void someMethod() {
    Messagebox.show("Question?", "Title",
            new Messagebox.Button[]{Messagebox.Button.YES, Messagebox.Button.NO},
            Messagebox.QUESTION,
            event -> {
                if (Messagebox.Button.YES.equals(event.getButton())) {
                    // ...
                    BindUtils.postNotifyChange(null, null, this, "yourIDhere");
                }
            });
}
link publish delete flag offensive edit

Question tools

Follow
1 follower

RSS

Stats

Asked: 2018-01-02 14:27:30 +0800

Seen: 22 times

Last updated: Jan 03 '18

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