Think about in Java, how do we use the observer pattern to make the view reflect the changes in the model? Observers, observers, registering listeners, events, callbacks, getting data, etc. are you having trouble with them?
With the binding, we can directly bind an attribute of the observer to one of the attributes of the observer, synchronizing the changes, saving a number of steps in the middle, and in the following example, I bound the transparency of a circle and the value of a text to a value of a slider. See what happens when you run in your browser (note: Java plug-ins are required, otherwise no effect is seen)
A slider
var slider = SwingSlider {
minimum: 0
maximum: 100
value: 80
vertical: false
};
//主窗体
Stage {
title: "MyApp"
scene: Scene {
width: 200
height: 200
content: [
VBox{
spacing: 10
content: [
//加入滑块
slider,
//加入一个圆
Circle {
centerX: 100
centerY: 50
radius: 40
fill: Color.RED
//将透明度和滑块的值绑定
opacity: bind {
slider.value / 100.0
}
},
//加入一个文本
Text {
font: Font {
size: 24
}
x: 10,
y: 30
// 将文本内容和滑块的值绑定
content: bind "{slider.value}"
}
]
}
]
}
};
Bind can be binding not only to variables, but also to expressions, functions, and so on, and can be bound in both directions.
As for triggers, personal sensations are similar to a Setter, and when you assign a variable, do something extra, because when you create an object in a literal way it is impossible to invoke any function, setting this language feature may be more of a consideration.