From e09727f13ef2c0cdbaf4ec7b49bd680662fafaff Mon Sep 17 00:00:00 2001 From: Furkan Uzumcu Date: Thu, 9 May 2019 10:25:38 -0400 Subject: [PATCH] Add a section for transient bindings --- README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/README.md b/README.md index 3f83aa5..d65343a 100644 --- a/README.md +++ b/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.