vaadin / flow

Vaadin Flow is a Java framework binding Vaadin web components to Java. This is part of Vaadin 10+.
Apache License 2.0
600 stars 165 forks source link

Allow HTML templates without requiring Polymer.Element registration #3566

Open marcushellberg opened 6 years ago

marcushellberg commented 6 years ago

I want to be able to create an HTML template with data binding, without having to define a full-blown Polymer element with all its boilerplate. This should be an alternative to the current way of always requiring a full element.

I think that this would simplify a majority of use cases. It would also make the choice of doing mixed client/server development more explicit.

Enabling HTML templates without Polymer boilerplate would also make migrating to Polymer 3 easier, as no end user code is explicitly using it.

Mockup code for how I would like this to work:

MyElement.java:

@Tag("my-element")
@HtmlTemplate("my-element.html")
@PolymerImport("vaadin-button")
class MyElement extends PolymerTemplate<MyElement.Model> {
  private int buttonClicks = 0;
  interface Model {
    void setText(String text);
  }

 @ClientDelegate
 void countClicks() {
    getModel().setText(String.format("You have clicked %d times!", ++buttonClicks);
  }
}

my-element.html. Only the content of the <template> tag, Flow would take care of actually wrapping this up in a custom element.

<vaadin-button on-click="countClicks">Click me</vaadin-button>
<div>[[text]]</div>

I'm not sure how template dependencies should be handled. Here, I mocked it with @PolymerImport().

jojule commented 6 years ago

If Flow in any case reads through the HTML template (as it must), it can surely scan for custom elements used and auto-import them. (@PolymerImport("vaadin-button") did not add more info than what is already visible from the HTML)

Legioth commented 6 years ago

We can even go slightly further without introducing very much additional magic:

Some other observations:

knoobie commented 5 years ago

I would consider this as an actual replacement for CustomLayout. (thumbs up)