# ljianshu/mvvm architecture

> Architecture diagram of the GitHub repository ljianshu/mvvm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ljianshu/mvvm
- Repository: https://github.com/ljianshu/mvvm

## Overview

- MVVM Entry Point: This is the constructor (found in mvvm.js) that ties everything together. It initializes data observation, sets up the template compilation, and proxies data properties.
- Compile: (in compile.js) This component parses the template (HTML in mvvm.html and vue-demo/index.html) for directives (such as “v-model”, “v-text”, “v-on”) and text interpolation ({{ }}) and sets up the initial rendering while binding update callbacks. Name it “Template Compiler.”
- Watcher: (in watcher.js) This is the subscriber or bridge that listens for changes (by registering with Dep) and invokes the update functions to refresh the view. Label it “Data Watcher.”

##### Their Relationships and Data Flow
- That the MVVM constructor initializes by proxying properties and handing off the data object to Observer.
- The Compile module should be shown as taking the DOM (or a DocumentFragment of it), scanning for directives and binding them via new Watcher instances.
- That Watcher instances subscribe to changes in a given property (demonstrating the “publish-subscribe” pattern) so that when the Observer detects a data change, the corresponding Dep notifies all its Watchers.

##### Architectural Patterns and Design Principles
- That the project uses the Observer pattern combined with a simplified publish-subscribe mechanism (via Dep) to achieve two-way data binding.
- That it uses data proxying on the MVVM instance to simplify access to data properties.
- Use of separation of concern: Observer for data listening, Compile for template parsing and binding, and Watcher for updating upon data changes.

##### Relevant Technologies and Files
- Mention that the binding is implemented using modern JavaScript features such as Object.defineProperty().
- You might include a small section for the “View” which comes from the HTML files (mvvm.html and vue-demo/index.html), where the DOM elements are used to demonstrate the binding (including input fields, paragraphs, and button elements).
- That CSS and image directories are present, indicating UI styling and assets, though their role is minimal compared to the core MVVM logic.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interaction
    U["User Input (View)"]:::view

    %% MVVM Core Subgraph
    subgraph "MVVM Core"
        MVVM["MVVM Constructor"]:::core
        OBS["Data Observer"]:::core
        DEP["Dep (Dependency Manager)"]:::util
        COMP["Template Compiler"]:::core
        WATCH["Data Watcher"]:::core
    end

    %% View Layer Subgraph
    subgraph "View Layer"
        HTML1["mvvm.html"]:::view
        HTML2["vue-demo/index.html"]:::view
    end

    %% Data Flow
    U -->|"triggers"| MVVM
    MVVM -->|"proxiesData&init"| OBS
    OBS -->|"attachesDeps"| DEP
    COMP -->|"bindsTemplate"| WATCH
    DEP -->|"notifies"| WATCH
    WATCH -->|"updatesView"| COMP
    COMP -->|"rendersUpdates"| HTML1
    COMP -->|"rendersUpdates"| HTML2
    HTML1 -->|"userInput"| MVVM
    HTML2 -->|"userInput"| MVVM

    %% Click Events
    click MVVM "https://github.com/ljianshu/mvvm/blob/master/js/mvvm.js"
    click OBS "https://github.com/ljianshu/mvvm/blob/master/js/observer.js"
    click COMP "https://github.com/ljianshu/mvvm/blob/master/js/compile.js"
    click WATCH "https://github.com/ljianshu/mvvm/blob/master/js/watcher.js"
    click HTML1 "https://github.com/ljianshu/mvvm/blob/master/mvvm.html"
    click HTML2 "https://github.com/ljianshu/mvvm/blob/master/vue-demo/index.html"

    %% Styles
    classDef core fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef util fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef view fill:#fff3cd,stroke:#856404,stroke-width:2px;
```
