Add a section for transient bindings

This commit is contained in:
Furkan Uzumcu
2019-05-09 10:25:38 -04:00
parent 2f37b0aa78
commit e09727f13e

View File

@@ -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.