Schema managers

Schema managers are distinct classes integrated with the schema parser. They are responsible for managing a specific root level property name designated by the parser. Often, these managers are used in conjunction with specialized providers. To illustrate, let’s consider the template manager as an example.

The schema allows for a root level property named “templates,” which is handled by the templates manager. Templates represent smaller segments of the UI that can be utilized in various parts of the schema, aiming to facilitate reusable markup. This approach reduces schema size by minimizing repetition.

While the templates manager is capable of generating templates, the challenge lies in effectively employing this manager. This is where the template provider plays a role. When encountering an element of the type “template,” the template provider uses the ID specified in the schema to generate the appropriate code snippet.

Schema example

{
    "templates": [
        {
            "id": "example",
            "elements": [
                {
                    "element": "button",
                    "attributes": {
                        "type": "submit"
                    }
                }
            ]
        }
    ],

    "body": {
        "elements": [
            {
                "element": "template",
                "id": "example"
            }
        ]
    }
}

Template manager example

All managers must have a set of default members.

  1. A read-only “key” property is essential, which specifies the schema root level property the manager is responsible for.
  2. There is an ‘initialize’ method designed to set up the manager prior to parsing the schema. All managers possess a “parser” property, and the parser in turn contains a “schema” property, representing the current JSON schema being parsed.
  3. Additionally, a ‘reset’ method is implemented to release resources once processing is complete.
export default class TemplatesManager extends BaseManager {
    #templates;

    get key() {
        return "templates"
    }

    async reset() {
        // clean up resources
    }

    async initialize() {
        this.#templates = this.parser.schema[this.key];
    }

    async getTemplate(store, id) {
        if (this[store][id] == null) {
            throw new Error(`There is no template in the schema for with id "${id}"`);
        }
        
        return this[store][id];
    }
}

Because the providers are responsible for generating the HTML, no extra effort is needed in this aspect. The main task is to verify if a template has been defined and then retrieve it for the provider’s use. The provider is aware of the manager’s existence and understands that it possesses a ‘getTemplate’ method.

The established convention dictates that the manager operates independently, without knowledge of external elements. Conversely, the provider is informed about the manager and is aware of the methods it offers for utilization.