Package com.vaadin.flow.component
Class SignalPropertySupport<T>
java.lang.Object
com.vaadin.flow.component.SignalPropertySupport<T>
- Type Parameters:
T- the type of the property
- All Implemented Interfaces:
Serializable
Helper class for binding a
Signal to a property of a
Component. Not all component features delegate directly to the state
in Element. For those features, this helper class
ensures state management behaves consistently with Element properties.
Example of usage:
ValueSignal<String> signal = new ValueSignal<>("");
MyComponent component = new MyComponent();
add(component);
component.bindTextContent(signal);
signal.set("Hello"); // component content showing now "Content: Hello" text
@Tag(div)
public class MyComponent extends Component {
private final SignalPropertySupport<String> textProperty = SignalPropertySupport
.create(this, value -> {
getElement().executeJs("this.textContent = 'Content: ' + $0",
value);
});
public String getText() {
return textProperty.get();
}
public void setText(String text) {
textProperty.set(text);
}
public void bindTextContent(Signal<String> textSignal) {
textProperty.bind(textSignal);
}
}
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidBinds aSignal's value to this property support and keeps the value synchronized with the signal value while the component is in attached state.static <T> SignalPropertySupport<T> create(Component owner, SerializableConsumer<T> valueChangeConsumer) Creates a new instance of SignalPropertySupport for the given owner component and a value change consumer to be called when property value is updated.get()Gets the current value of this property support.voidSets the value of this property support.
-
Method Details
-
create
public static <T> SignalPropertySupport<T> create(Component owner, SerializableConsumer<T> valueChangeConsumer) Creates a new instance of SignalPropertySupport for the given owner component and a value change consumer to be called when property value is updated. The property value is updated either manually withset(Object), or automatically viaSignalvalue change while the owner component is in the attached state and signal is bound withbind(Signal).- Type Parameters:
T- the type of the property- Parameters:
owner- the owner component for which the value change consumer is applied, must not be nullvalueChangeConsumer- the consumer to be called when the value changes, must not be null- Returns:
- a new instance of SignalPropertySupport
- See Also:
-
bind
Binds aSignal's value to this property support and keeps the value synchronized with the signal value while the component is in attached state. When the component is in detached state, signal value changes have no effect.nullsignal unbinds existing binding.While a Signal is bound to a property support, any attempt to set value manually throws
BindingActiveException. Same happens when trying to bind a new Signal while one is already bound.- Parameters:
signal- the signal to bind ornullto unbind any existing binding- Throws:
BindingActiveException- thrown when there is already an existing binding
-
get
Gets the current value of this property support.- Returns:
- the current value
-
set
Sets the value of this property support.- Parameters:
value- the value to set- Throws:
BindingActiveException- thrown when there is an existing binding
-