Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:AGenUI AGenUI ComponentModel

From Leeroopedia
Knowledge Sources
Domains UI_Framework, Data_Binding
Last Updated 2026-10-24 17:21 GMT

Overview

Concrete component data model representing a single node in the virtual DOM, with typed attributes, data binding subscriptions, children, and template expansion support, provided by the AGenUI C++ core.

Description

The `ComponentModel` class represents one component in the virtual DOM tree. It is created by `ComponentManager::parseComponent()` from A2UI protocol JSON and holds:

  • `_id` / `_rawId`: component identifier (protocol ID and raw ID)
  • `_component`: type name (e.g. "Text", "Button", "MyCustom")
  • `_attributes`: map of attribute name → `shared_ptr<DataValue>` (typed values: static, data binding, function call, action)
  • `_children`: list of child component IDs
  • `_surfaceContext`: `ISurfaceContext*` for data model access
  • `_observer`: `IComponentChangedObserver*` for change notification
  • `_templateComponentGenerator`: for list child generation
  • `_attributeBinders`: map of attribute name → `AttributeDataBinder` (data binding subscriptions)

The `setAttribute(key, value)` method stores a `DataValue` and, if it's a data-binding value, creates an `AttributeDataBinder` that subscribes to the data model path. When the data at that path changes, the binder calls `onComponentAttributeDataChanged`, triggering a re-evaluation and diff emission.

`ComponentModel` implements `IComponentAttributeDataChangedObserver`, `ISpecApplicable`, and `IDataValueContext` interfaces, making it the central hub for data binding, spec application, and dynamic value resolution within a component.

Usage

  • Internal to SDK: created by `ComponentManager` during protocol parsing; not directly instantiated by host code.
  • Data binding: attributes with `{"path": "..."}` values automatically subscribe to the data model.
  • Custom components: deep-parse properties (registered via `registerDeepParseProperty`) are resolved recursively during `setAttribute`.

Code Reference

Source Location

  • Repository: AGenUI
  • Header: `core/src/surface/component_manager/agenui_component_model.h:L98-154`
  • Implementation: `core/src/surface/component_manager/agenui_component_model.cpp` (536 lines)

Signature

// core/src/surface/component_manager/agenui_component_model.h
namespace agenui {

class ComponentModel : public IComponentAttributeDataChangedObserver,
                       public ISpecApplicable,
                       public IDataValueContext {
public:
    ComponentModel(const std::string& id, const std::string& rawId,
                   const std::string& component,
                   ISurfaceContext* surfaceContext,
                   IComponentChangedObserver* observer,
                   ITemplateComponentGenerator* generator);

    // Set an attribute value (creates data binder for binding values)
    void setAttribute(const std::string& key, std::shared_ptr<DataValue> value);

    // Synchronize a bound data attribute with a new value (user input)
    void syncValue(const std::string& attributeName, const std::string& value);

    // Execute the component's action (event or function call)
    void executeAction(const std::string& surfaceId,
                      EventDispatcher* dispatcher,
                      const std::string& contextJson = "");

    // Getters
    std::string getId() const;
    std::string getRawId() const;
    std::string getComponent() const;
    const std::map<std::string, std::shared_ptr<DataValue>>& getAllAttributes() const;
    const std::vector<std::string>& getChildren() const;
    int getInstanceId() const;

    // ... (full interface in source)
};

} // namespace agenui

Import

#include "surface/component_manager/agenui_component_model.h"

I/O Contract

Inputs

Name Type Required Description
id `const std::string&` Yes Component ID from A2UI protocol
rawId `const std::string&` Yes Raw ID
component `const std::string&` Yes Type name (e.g. "Text", "MyCustom")
surfaceContext `ISurfaceContext*` Yes Surface context for data access
observer `IComponentChangedObserver*` Yes Change notification target
generator `ITemplateComponentGenerator*` No List child generator (nullptr for non-list)

Outputs

Name Type Description
(instance) Component with attributes, children, data bindings
Attribute changes onComponentAttributeDataChanged triggers re-evaluation

Usage Examples

Internal Component Creation (SDK Internal)

// Created by ComponentManager::parseComponent() from A2UI protocol JSON:
// {"component":"Text","id":"t1","text":{"path":"user.name"},"styles":{"fontSize":14}}

auto model = std::make_shared<ComponentModel>(
    "t1",        // id
    "t1",        // rawId
    "Text",      // component type
    surfaceCtx,  // ISurfaceContext*
    observer,    // IComponentChangedObserver*
    nullptr      // ITemplateComponentGenerator* (not a list)
);

// Set attributes (DataValueParser converts JSON to DataValue)
model->setAttribute("text", DataValueParser::parseDataValue(model, R"({"path":"user.name"})"));
model->setAttribute("styles", DataValueParser::parseDataValue(model, R"({"fontSize":14})"));

// When data model "user.name" changes, the binder fires:
// → observer->onComponentAttributeDataChanged("text", false)
// → re-evaluate → emit diff to native renderer

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment