mirror of
https://github.com/allankoechke/QML-Coding-Guide.git
synced 2026-12-02 01:09:43 +00:00
Add a section for transient bindings
This commit is contained in:
33
README.md
33
README.md
@@ -15,6 +15,7 @@
|
||||
- [Reduce the Number of Bindings](#reduce-the-number-of-bindings)
|
||||
- [Making `Connections`](#making-connections)
|
||||
- [Use `Binding` Object](#use-binding-object)
|
||||
+ [Transient Bindings](#transient-bindings)
|
||||
- [KISS It](#kiss-it)
|
||||
- [Be Lazy](#be-lazy)
|
||||
- [Avoid Unnecessary Re-Evaluations](#avoid-unnecessary-re-evaluations)
|
||||
@@ -574,6 +575,38 @@ ColorDialog {
|
||||
}
|
||||
```
|
||||
|
||||
#### Transient Bindings
|
||||
|
||||
There may be cases where you have to end up using an imperative assignment. But
|
||||
naturally this will break the binding. In that case, you can create transient
|
||||
`Binding` objects to safely set the new property without breaking the existing
|
||||
binding.
|
||||
|
||||
```qml
|
||||
Item {
|
||||
property var contentItem
|
||||
|
||||
onContentItemChanged: {
|
||||
contentItem.width = 100 // This will break the binding.
|
||||
// -----
|
||||
var temp = cmpBinding.createObject(root, {
|
||||
"target": contentItem,
|
||||
"property": "width",
|
||||
"value": 100
|
||||
})
|
||||
// Now the width property is safely updated to 100 without breaking
|
||||
// any existing bindings.
|
||||
temp.destroy() // Don't forget to destroy it.
|
||||
}
|
||||
|
||||
Component {
|
||||
id: cmpBinding
|
||||
|
||||
Binding { }
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### KISS It
|
||||
|
||||
You are probably already familiar with the [KISS principle](https://en.wikipedia.org/wiki/KISS_principle). QML supports optimization of binding expressions. Optimized bindings do not require a JavaScript environment hence it runs faster. The basic requirement for optimization of bindings is that the type information of every symbol accessed must be known at compile time.
|
||||
|
||||
Reference in New Issue
Block a user