Files
Unify/Property_Binding.md

65 lines
3.6 KiB
Markdown
Raw Normal View History

2025-12-25 11:16:59 +01:00
### Automatic Rendering and Property Binding in Unify
In Unify, you **define properties** on your objects, and the framework automatically **creates the associated DOM elements**, applies **styles**, and **binds the properties** without you needing to manually manipulate the DOM. This approach significantly reduces boilerplate code, making it easier and faster to build dynamic user interfaces.
#### Key Concepts:
1. **Automatic Element Creation**:
When you define a Unify object, like a `panel`, the framework automatically generates the corresponding HTML element for you. You don't need to manually call `document.createElement()` or manage the element's attributes.
2. **Property Binding**:
The properties you define (such as `height`, `width`, or `text`) are automatically reflected in the rendered DOM element. For instance, if you set the `height` property of a `panel` object, Unify ensures that the corresponding HTML element's height is updated accordingly.
3. **No Need for Render Methods**:
Unlike traditional frameworks where you need to explicitly define a `render()` method to apply changes to the DOM, Unify handles this for you. Simply updating the properties of a Unify object triggers the necessary updates in the DOM without needing extra code.
4. **Dynamic Updates**:
Whenever a property is changed, such as adjusting the `height` or `text` of an element, Unify automatically re-renders the element to reflect these changes. There's no need to manually call DOM manipulation methods like `element.style.height` or `element.innerText`.
#### Example:
Heres an example demonstrating how Unify makes it easy to manage UI elements:
```js
class panel {
// Define initial properties
height = 100;
width = 200;
margin = 20;
text = "Initial Text";
// Modify properties dynamically
click() {
this.height = 300; // Change height
this.width = 400; // Change width
this.text = "Updated Text"; // Change text content
}
}
// Create an instance of the panel
const myPanel = new panel();
```
### How It Works:
1. **No Manual DOM Updates**: You dont need to manually create elements or apply styles. Unify automatically creates an HTML element for your `panel` and applies the `height`, `width`, and `text` properties to it.
2. **Automatic Synchronization**: When you modify properties like `height`, `width`, or `text`, Unify ensures that the corresponding DOM element is updated to reflect these changes without requiring additional code.
3. **Simplified UI Development**: This approach makes it easier to build dynamic, interactive UIs. You simply update the objects properties, and Unify handles the underlying DOM updates.
4. **Declarative Approach**: By defining properties on objects, you declaratively specify the state of your UI, and Unify takes care of the rendering and synchronization for you.
### Benefits:
* **Less Boilerplate Code**: Since Unify automatically binds properties to DOM elements, there's no need for repetitive code to manually update styles or text.
* **Faster Development**: By removing the need for manual DOM manipulation, you can focus on defining the state and behavior of your components.
* **Automatic Synchronization**: Changes to object properties are automatically reflected in the UI without any extra code to update the DOM.
---
This approach streamlines the process of building dynamic UIs and ensures that your objects and the DOM are always in sync, reducing the likelihood of errors and improving maintainability. With Unify, you focus on defining the state of your UI, and the framework takes care of the rendering for you.