ValueChangeListener fires on page load

Hello again.

I’ve added a few changevaluelisteners to fields on my page, so that I can the contents of other fields when the listened fields change.

The only trouble is that the change listeners get fired when the page loads, which I don’t want them to do.

public class ManuscriptEdit extends AbstractEditor<Manuscript> {

    @Named("manuscript_journal")
    LookupField manuscriptJournal;

    @Named("manuscript_journal_volume")
    LookupField manuscriptJournalVolume;

    @Named("manuscript_journal_issue")
    LookupField manuscriptJournalIssue;

    @Named("manuscript_process")
    LookupField manuscriptProcess;

    @Override
    public void init(Map<String, Object> params) {
        super.init(params);

        manuscriptJournal.addValueChangeListener(e -> {
            
            manuscriptJournalVolume.setValue(null);
            manuscriptJournalIssue.setValue(null);

            // If the journal is changed then we want to set the manuscript
            // process to the new journal default.
            Journal journal = (Journal) e.getValue();
            Process process = journal.getDefaultProcess();
            manuscriptProcess.setValue(process);

        });

        manuscriptJournalVolume.addValueChangeListener(e -> {

            manuscriptJournalIssue.setValue(null);

        });

    }
}

This means that my default process gets reset every time the page loads, but weirdly enough, the JournalVolume and JournalIssue don’t seem to get set to null, even thought the instructions are executed (not sure why that is).

Am I doing this right, or should I be doing it some other way (maybe listeners on the datasources?)

If I am doing it right, then how do I stop the events from being fired until the first change is made by the user?

Solved!

If I create the handlers in the ready() method then they won’t run before the page has finished loading the fields. So don’t use the init() method; override the ready() method instead.

All good!

1 Like

Hi, @ray1

yes, you’re right. The reason is that data is loaded after the init method execution and of course this process will fire your listeners.

Regards,
Daniil.

2 Likes