RendererUtil { public static <T> void registerEventHandlers(Renderer<T> renderer, Element contentTemplate, Element templateDataHost, ValueProvider<String, T> keyMapper) { Map<String, SerializableConsumer<T>> eventHandlers = renderer .getEventHandlers(); if (!eventHandlers.isEmpty()) { runOnAttach(templateDataHost.getNode(), () -> processTemplateRendererEventHandlers( getUI(templateDataHost), templateDataHost, eventHandlers, keyMapper)); runOnAttach(contentTemplate.getNode(), () -> getUI(contentTemplate) .getInternals().getStateTree() .beforeClientResponse(templateDataHost.getNode(), context -> context.getUI().getPage().executeJs( "$0.__dataHost = $1;", contentTemplate, templateDataHost))); } } private  RendererUtil(); static void registerEventHandlers(Renderer<T> renderer,
            Element contentTemplate, Element templateDataHost,
            ValueProvider<String, T> keyMapper);  }
@Test public void registerEventHandlers_elementsAreAlreadyAttached_setupEvenHandlers() { UI ui = new TestUI(); Element contentTemplate = new Element(Tag.DIV); Element templateDataHost = new Element(Tag.SPAN); attachElements(ui, contentTemplate, templateDataHost); TestUIInternals internals = (TestUIInternals) ui.getInternals(); Renderer<String> renderer = new Renderer<>(); renderer.setEventHandler("foo", value -> { }); RendererUtil.registerEventHandlers(renderer, contentTemplate, templateDataHost, ValueProvider.identity()); assertJSExecutions(ui, internals, contentTemplate, templateDataHost); }
@Test public void registerEventHandlers_setupEvenHandlersOnAttach() { UI ui = new TestUI(); TestUIInternals internals = (TestUIInternals) ui.getInternals(); Renderer<String> renderer = new Renderer<>(); renderer.setEventHandler("foo", value -> { }); Element contentTemplate = new Element(Tag.DIV); Element templateDataHost = new Element(Tag.SPAN); RendererUtil.registerEventHandlers(renderer, contentTemplate, templateDataHost, ValueProvider.identity()); attachElements(ui, contentTemplate, templateDataHost); assertJSExecutions(ui, internals, contentTemplate, templateDataHost); ui.getElement().removeAllChildren(); internals.invocations.clear(); attachElements(ui, contentTemplate, templateDataHost); assertJSExecutions(ui, internals, contentTemplate, templateDataHost); }
@Test public void registerEventHandlers_attachMultipleTimes_singleEventListenerRegistered() { UI ui = new TestUI(); TestUIInternals internals = (TestUIInternals) ui.getInternals(); Renderer<String> renderer = new Renderer<>(); AtomicInteger eventCounter = new AtomicInteger(0); renderer.setEventHandler("foo", value -> { eventCounter.getAndIncrement(); }); Element contentTemplate = new Element(Tag.DIV); Element templateDataHost = new Element(Tag.SPAN); RendererUtil.registerEventHandlers(renderer, contentTemplate, templateDataHost, ValueProvider.identity()); attachElements(ui, contentTemplate, templateDataHost); attachElements(ui, contentTemplate, templateDataHost); internals.getStateTree().runExecutionsBeforeClientResponse(); JsonObject eventData = Json.createObject(); eventData.put("key", ""); templateDataHost.getNode().getFeature(ElementListenerMap.class) .fireEvent(new DomEvent(templateDataHost, "foo", eventData)); Assert.assertEquals(1, eventCounter.get()); }