Skip to content

Migration

tech_e edited this page Jan 11, 2025 · 6 revisions

Migration Notes for base 2 to base 3

This document serves as a comprehensive guide for software developers who are planning to migrate from version 2 of the "base" library to its latest iteration, version 3. The primary objective of this migration is to ensure a smooth transition, although it's important to be aware that several changes have been implemented to bolster code consistency. Some of these changes may necessitate adjustments to existing code. Ultimately, these modifications are geared towards enhancing the maintainability of future code.

Core Changes

In version 3, significant revisions have been made to the core "base" object, which acts as the central hub for accessing all modules and functions. The most notable core changes are as follows:

  • Module Reduction: To streamline the core object, we have minimized the number of modules incorporated. Notably, certain modules have been omitted.

  • DOM Methods: Functions related to the Document Object Model (DOM), encompassing data manipulation, CSS, attributes, width, height, offset, and position, have been relocated to the Dom module. Developers are now expected to utilize the Dom module to access these methods.

  • Refactored Core Object: The core object itself has undergone a comprehensive restructuring. It has been modularized into shared modules, including Strings, Arrays, Objects, and Types.

DataTracker

The dataTracker object has undergone significant refactoring, resulting in the creation of the DataTracker class. Notably, its methods have been transformed into static methods.

Ajax

To align with the naming conventions of other modules, the ajax object has been converted to uppercase, now referred to as "Ajax."

HtmlBuilder

The functionality previously associated with the HtmlBuilder has been meticulously refactored and is now an integral part of the Html module. This transformation includes the conversion of its methods into static functions. Furthermore, certain method names have been modified to ensure consistency.

Layout

The LayoutBuilder has been rebranded as "Builder." Its methods have been adapted into static functions. It is imperative to note that the Builder class no longer inherits from the HtmlBuilder class. Instead, it employs the HtmlHelper class, an extension of the Html module. This alteration simplifies the process of constructing layouts.

Additionally, the LayerParser has undergone a refactoring process, resulting in the creation of the Parser class. Its methods have been converted into static functions.

Directives

A substantial overhaul has been executed in the realm of directives. These directives have been extricated from the LayoutBuilder and transplanted into the Directives module. Furthermore, the directives themselves have undergone a meticulous refactoring process to ensure uniformity.

The onSet, onState, and watcher callbacks have experienced modifications in their parameters. They now transmit the "value" as the initial parameter and the "ele" as the secondary parameter.

New Directive: onDestroyed

An exciting addition to the repertoire is the introduction of a new directive, known as onDestroyed. This directive comes into play when an element is removed from the Document Object Model (DOM).

Data Modules Update

The Data modules have been revamped to incorporate the newly introduced DataProxy class. This enhancement empowers developers to bypass the legacy getter and setter methods. Instead, they can now employ dot notation to retrieve and manipulate data, with the added benefit of changes being automatically published to the dataBinder.

// Old data
data.get('name');
data.set('name', 'test');
// New data
data.name;
data.name = 'test';

Watcher Directive

A noteworthy inclusion in the update is the introduction of the watcher directive. This directive facilitates the monitoring of any attribute. To implement this functionality, developers are required to encapsulate a property within double square brackets "." This action initiates a watcher for the property, resulting in the automatic update of the associated attribute when the property undergoes modifications. This feature can be seamlessly applied to a wide range of attributes, including class, href, src, and text content.

// New watcher
Div({class: '[[propName]]'})
// Multi-attribute watching
A({href: '/account/user/[[userId]]'}, [[userName]] and [[age]]')
// Custom data watching
Div({class: ['[[propName]]', data]})
// Multi-data watcher
Div({class: [[[propName]] [[otherPropName]]', [data, otherData]]})
// With callback
const callBack = ([propName, otherProp]) =>
{
    const active = propName === true? 'active' : 'inactive';
    return `${active} ${otherProp}}`;
};

Div({class: [[[propName]] [[otherPropName]]', [data, otherData], callBack]})

State

The state object has undergone a comprehensive refactoring process, culminating in the creation of the StateTracker class. Its methods have been converted into static methods.

Date

A pivotal modification is the refactoring of the date object, which has evolved into the DateTime object.

Atom

One of the standout transformations is the transition of the atom object. It no longer accommodates inheritance. Instead, the Atom class has been subjected to a paradigm shift, relying on a callback function. This callback function returns both props and children. Developers now have the flexibility to include optional arguments, reducing the verbosity of code. The order of presentation comprises props followed by children. However, it is acceptable to omit props if only children are present. When dealing with children, they should be enclosed within an array, unless a sole text content string is involved, in which case, the string value can be directly passed. It is imperative that atoms be structured using composition instead of inheritance.

// Atom Example
const Div = Atom((props, children) =>
{
    return {tag: 'div', ...props, children};
});

// props only
let layout = Div({class: 'text'});

// text child only
layout = Div('test');

// array child only
layout = Div([]);

// props and text child
layout = Div({class: 'text'}, 'test');

// props and array child
layout = Div({class: 'text'}, [
    Div('test'),
    Div('test')
]);

Version 2.6.0 atoms

// Atom Composition Example
Section({
    nest: [
        Article({
            class: 'post',
            nest: [
                Header({
                    nest: [
                        H1({
                            text: 'Title'
                        })
                    ]
                })
            ]
        })
    ]
})

Next version atoms

// Next Version Atom Composition Example
Section([
    Article({ class: 'post' }, [
        Header([
            H1('Title')
        ])
    ])
])

Components

Components have been harmonized with the atoms in terms of their argument pattern. Props precede children, although it is permissible to exclude props when dealing exclusively with children. Children should be wrapped within an array unless they are represented by a solitary text content string, in which case, the string value can be directly provided. It's important to emphasize that the component class no longer accommodates builder methods. Instead, the Builder class should be utilized for constructing layouts. The cacheable directive has been rebranded as cache, aligning it with the naming conventions of atom directives. Notably, the "cache" method has been deprecated. The Builder class now exclusively handles component rendering via the "render" method, which returns both the layout and the component instance. It's essential to be aware that the setup method does not encompass component rendering.

Import

A notable introduction to the framework is the new Import module, which facilitates dynamic module loading. This feature empowers developers to load modules on demand. To leverage this functionality, the imported module must be the default export of the respective module.

// Code split your app using import
Import({
    src: '/module/path.js'
})

Clone this wiki locally