Events and reactions

Every important, concurrent action in SWAC fires events when it changes it's state. Those events can be used to execute custom code at the right time.

SWAC core lifecycle events

The SWAC core fires events that signal the state of SWACs core functionallity.
The following table contains the core lifecycle events in typically occuring order.

SenderEvent nameExplanation
swac.jsswac_DEPENDENCY.id_dependency_loadedA core dependency (library) was loaded.
swac.jsswac_core_loadedSWAC core is loaded. Reactions can be registered now. DataRequestors can use Model now.
swac.jsswac_components_completeFired when all components have loaded.

Component lifecycle events

Each component has it's own lifecycle and informs on state chances over events.
The following table contains all component lifecycle events in a typically occuring order.

SenderEvent nameExplanation
Component.jsswac_REQUESTOR.ID_plugins_loadedFired when all component plugins have loaded.
ComponentHandler.jsswac_REQUESTOR.ID_inactiveFired by the requestor when option 'activeOn' resolves to inactive.
ComponentHandler.jsswac_REQUESTOR.ID_loadedFired when the component has completed it's init() but before data is added.
Component.jsswac_Component_REQUESTOR.ID_setAddedFired when a dataset was added.
View.jsswac_REQUESTOR.ID_repeatedForSetFired when a new gui element was created for an added set. Carries requestor, repeateds (DOM Elements), and set
Component.jsswac_Component_REQUESTOR.ID_lastSetFromRequestAddedFired when the last set of a data request was added.
Component.jsswac_REQUESTOR.ID_reloadedFired when one component has reloaded.
View.jsswac_REQUESTOR.ID_inviFired when the requestors DOM element enters or leaves the viewport (e.g. visibility to user changes)
View.jsswac_REQUESTOR.ID_endviFired when the user reaches the end of the components view by scrolling down. Also fires if the end leaves the viewport.

Data lifecycle events

Data has it's own lifecycle. During this data lifecycle events are send.

SenderEvent nameExplanation
Remote.jsswac_fetchfail_STATUSCODEFired when an fetch attempt fails. STATUSCODE is the HTTP status code. (e.g. 401)

Other general events

Language file handling and message generating also create events.

SenderEvent nameExplanation
Languageswac_lang_loadedFired when a language was loaded. Contains language abbrivation in "lang" and list of translated requestors.
Msgswac_msgA logger message was sent. The details contain level (info,warn,error) and msg
swacswac_serviceworker_msgFired when the serviceworker sends a message. Contains the message content in detail.

Component events

Components have own events. See the components documentation for component events.


// Register event handler
document.addEventListener('swac_' + requestor.id + '_reloaded', function (evt) {
    // Do something here
});
        

Online events

SWAC brings support for actions performed when a client loses his internet connection, the server is not reachable or the client goes online again.
Online / offline functions either work together with the progressive functionality and / or without it.

Offline / Online informations

The user becomes an information if his client goes offline or the connection to the backend is lost, so no user must wonder why something is not working. You can deactiate this by changeing the configuration:


                offlineNotify: false,

Extend your site with online / offline functions

Even without useing a component you can extend your site with online / offline functions by extending a simple class and adding a configuration.

Implementing a reaction set by extending the OnlineReaction class:

var MyReactFactory = {};
MyReactFactory.create = function (config) {
    return new MyReact(config);
};

class MyReact extends OnlineReaction {
    constructor(config) {
        super(config);
    }

    onOnline(evt) {
        console.log('Upload onOnline reaction');
    }

    onOffline(evt) {
        console.log('Upload onOffline reaction');
    }

    onUnreachable(evt) {
        console.log('Upload onUnreachable action');
    }
}
        
Write an entry to the configuration.js to make the actions available on all pages.

SWAC_config.onlinereactions[0] = {
    path: 'path/to/MyReact.js',
    config: {
       someopts: 'Specify some opts here for your config'
    }
};
        

Components with online / offline functionality

All swac components can be easily extended with functions that will be executed if client goes online / offline. It's done the same way like defining a OnlineReaction without component except that the script is located within the plugins directory.

Reactions

With Reactions you can register functions that will be executed when a requestors component is completely loaded.
Furthermore you can register functions that are executed when all requestors from a list are completely loaded. You just have to list the names of the requestors as second and so on parameter.

Combining components

Reactions allow it to combine components functunalities. So you can create a reaction that executes when the search and the present component are loaded and register code to update the presents data if a search was performed.

Example 1: Register Reaction to single requestor

The simplest useage is to wait for a component to be ready and than execute a function.


// Wait for swac reaction system to be ready
document.addEventListener('swac_core_loaded', function () {
    // Register reaction to the component with id "head_navigation"
    window.swac.reactions.addReaction(function () {
        alert("This is a reaction: head_navigation is loaded.");
    }, "head_navigation");
});
            

Example 2: Register Reaction to multiple requestors

A bit more advanced you can wait for two (or more) components and execute a function when they all are ready.


// Wait for swac reaction system to be ready
document.addEventListener('swac_core_loaded', function () {
    // Register reaction to the compoenents "selectobject" and "chart"
    window.swac.reactions.addReaction(function (requestors) {
        // Accessing the requestors
        let selectobjectreq = requestors['selectobject'];
        let chartreq = requestors['chart'];
        // Here you can do anything on those booth components
    }, "selectobject", "chart");
});