|
3 | 3 | import com.vaadin.flow.component.button.Button;
|
4 | 4 | import com.vaadin.flow.component.html.H1;
|
5 | 5 | import com.vaadin.flow.component.html.Paragraph;
|
| 6 | +import com.vaadin.flow.component.orderedlayout.HorizontalLayout; |
6 | 7 | import com.vaadin.flow.component.orderedlayout.VerticalLayout;
|
7 | 8 | import com.vaadin.flow.router.Route;
|
8 | 9 | import org.vaadin.addons.velocitycomponent.VElement;
|
9 | 10 |
|
10 | 11 | @Route
|
11 | 12 | public class VElementView extends VerticalLayout {
|
12 | 13 |
|
13 |
| - public record FooBar(String foo, String bar, String car) {} |
| 14 | + public VElementView() { |
| 15 | + add(new H1("Example to listen DOM events easily!")); |
14 | 16 |
|
15 |
| - public VElementView() { |
16 |
| - add(new H1("Example to listen DOM events easily!")); |
| 17 | + VElement.of(getElement()).on(FooBar.class, event -> { |
| 18 | + String bar = event.bar(); |
| 19 | + add(new Paragraph("Received foo-bar event with detail: " + event)); |
| 20 | + }); |
| 21 | + // Event name can also be specified explicitly |
| 22 | + VElement.of(getElement()).on("foo-car", FooBar.class, event -> { |
| 23 | + String bar = event.bar(); |
| 24 | + add(new Paragraph("Received foo-car event with detail: " + event)); |
| 25 | + }); |
| 26 | + VElement.of(getElement()).on("string-msg", String.class, event -> { |
| 27 | + add(new Paragraph("Received string-msg event with detail: " + event)); |
| 28 | + }); |
| 29 | + VElement.of(getElement()).on("boolean-msg", String.class, event -> { |
| 30 | + add(new Paragraph("Received boolean-msg event with detail: " + event)); |
| 31 | + }); |
| 32 | + VElement.of(getElement()).on("int-msg", Integer.class, event -> { |
| 33 | + add(new Paragraph("Received int-msg event with detail: " + event)); |
| 34 | + }); |
| 35 | + VElement.of(getElement()).on("double-msg", Double.class, event -> { |
| 36 | + add(new Paragraph("Received double-msg event with detail: " + event)); |
| 37 | + }); |
17 | 38 |
|
18 |
| - VElement.of(getElement()).on(FooBar.class, event -> { |
19 |
| - String bar = event.bar(); |
20 |
| - add(new Paragraph("Received foo-bar event with detail: " + event)); |
21 |
| - }); |
22 |
| - |
23 |
| - add(new Button("Click me", e -> { |
| 39 | + add(new HorizontalLayout( |
| 40 | + new Button("Click me", e -> { |
24 | 41 | getElement().executeJs("""
|
25 |
| - this.dispatchEvent(new CustomEvent('foo-bar', { |
26 |
| - detail: { |
27 |
| - foo: 'foo', |
28 |
| - bar: 'bar', |
29 |
| - car: 'car' |
30 |
| - } |
31 |
| - })); |
32 |
| - """); |
33 |
| - })); |
34 |
| - |
35 |
| - add(new Button("Click me too", e -> { |
| 42 | + this.dispatchEvent(new CustomEvent('foo-bar', { |
| 43 | + detail: { |
| 44 | + foo: 'foo', |
| 45 | + bar: 'bar', |
| 46 | + car: 'car' |
| 47 | + } |
| 48 | + })); |
| 49 | + """); |
| 50 | + }), |
| 51 | + new Button("Click me too", e -> { |
36 | 52 | // This is in theory bit more efficient for the server
|
37 | 53 | // as it does not need to deserialize-serialize (with GWT library)
|
38 | 54 | // and then desirialize with Jackson, but only once with Jackson
|
39 | 55 | getElement().executeJs("""
|
40 |
| - this.dispatchEvent(new CustomEvent('foo-bar', { |
41 |
| - detail: JSON.stringify({ |
42 |
| - foo: 'foo', |
43 |
| - bar: 'bar', |
44 |
| - car: 'car' |
45 |
| - }) |
46 |
| - })); |
47 |
| - """); |
48 |
| - })); |
49 |
| - |
50 |
| - } |
| 56 | + this.dispatchEvent(new CustomEvent('foo-car', { |
| 57 | + detail: JSON.stringify({ |
| 58 | + foo: 'foo', |
| 59 | + bar: 'bar', |
| 60 | + car: 'car' |
| 61 | + }) |
| 62 | + })); |
| 63 | + """); |
| 64 | + }), |
| 65 | + new Button("Fire string", e -> { |
| 66 | + getElement().executeJs(""" |
| 67 | + this.dispatchEvent(new CustomEvent('string-msg', { |
| 68 | + detail: 'foo' |
| 69 | + })); |
| 70 | + """); |
| 71 | + }), |
| 72 | + new Button("Fire boolean", e -> { |
| 73 | + getElement().executeJs(""" |
| 74 | + this.dispatchEvent(new CustomEvent('boolean-msg', { |
| 75 | + detail: true |
| 76 | + })); |
| 77 | + """); |
| 78 | + }), |
| 79 | + new Button("Fire int", e -> { |
| 80 | + getElement().executeJs(""" |
| 81 | + this.dispatchEvent(new CustomEvent('int-msg', { |
| 82 | + detail: 1 |
| 83 | + })); |
| 84 | + """); |
| 85 | + }), |
| 86 | + new Button("Fire double", e -> { |
| 87 | + getElement().executeJs(""" |
| 88 | + this.dispatchEvent(new CustomEvent('double-msg', { |
| 89 | + detail: 1.2345 |
| 90 | + })); |
| 91 | + """); |
| 92 | + }) |
| 93 | + )); |
| 94 | + |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + public record FooBar(String foo, String bar, String car) { |
| 99 | + } |
51 | 100 | }
|
0 commit comments