Skip to content
This repository has been archived by the owner on Apr 6, 2022. It is now read-only.

calling setItems causes a client-side exception #169

Closed
OlliTietavainenVaadin opened this issue Dec 11, 2018 · 2 comments · Fixed by #188
Closed

calling setItems causes a client-side exception #169

OlliTietavainenVaadin opened this issue Dec 11, 2018 · 2 comments · Fixed by #188
Labels
Milestone

Comments

@OlliTietavainenVaadin
Copy link
Member

OlliTietavainenVaadin commented Dec 11, 2018

vaadin.version 12.0.1
Setting the items to a previously-uninitialized ComboBox causes a client-side exception.
Simple demo that reproduces the issue:

public class MainView extends VerticalLayout {

    public MainView() {
        ComboBox<String> comboBox = new ComboBox<>();
        Button button = new Button("Click me to add items to the combobox",
                event -> comboBox.setItems("foo", "bar"));
        add(comboBox, button);
    }
}

Clicking the button causes a bunch of TypeErrors on the client side:

client-D1AD34905AC1AA5B4DBECA8FB0306D92.cache.js:197 (TypeError) : Cannot read property 'reset' of undefined

Workaround is to initialize the ComboBox with an empty Collection.

@Silver-Wulf
Copy link
Contributor

Is the intended functionality of this to have the ComboBox allow for setItems to work normally even if it is not yet initialized with a collection?

@Silver-Wulf
Copy link
Contributor

Commit #188 should fix this. I adapted your example code @OlliTietavainenVaadin to have another button to test the code added.

public MainLayout() {
		ComboBox<String> comboBox = new ComboBox<>();
		// comboBox.setItems(Collections.emptyList());
		Element e = comboBox.getElement();
		while (e.getParent() != null)
			e = e.getParent();
		final Element ele = e;
		Button button = new Button("Click me to run", event -> {
			if (comboBox.getElement().getProperty("$connector") == null) {
				ele.executeJavaScript("window.Vaadin.Flow.comboBoxConnector.initLazy($0);", comboBox.getElement());
			}
		});

		Button button2 = new Button("Click me to add items to the combobox", event -> comboBox.setItems("foo", "bar"));
		add(comboBox, button);
		add(comboBox, button2);
	}

Clicking the button to run the JS to initialize $connector seemed to fix the issue. The solution is kind of hack-y but works as far as I can tell.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.