Implementing Events

A short tutorial that shows how to implement events in alt-seven.

Events are a core concept in writing alt-seven-based applications. While it is not strictly necessary to use events at all, alt-seven is meant to be an event-driven framework,and using the publish/subcribe event system is part of that intention.

Configuration

In order to use events, you need to include them in the application startup so they are available. In LacesIDE, you can see the includes of the event modules in the app.js entry point of the application.

import {events} from '/js/app.events.js';
import {appLibraryEvents} from '/js/event/applibraries.js';
import {appEvents} from '/js/event/apps.js';
import {authEvents} from '/js/event/auth.js';
import {libraryEvents} from '/js/event/libraries.js';
import {mainEvents} from '/js/event/main.js';
import {menuEvents} from '/js/event/menu.js';
import {profileEvents} from '/js/event/profile.js';
import {sandboxEvents} from '/js/event/sandbox.js';
import {userEvents} from '/js/event/user.js';

//initialize pub/sub events
events();
appLibraryEvents();
appEvents();
authEvents();
libraryEvents();
mainEvents();
menuEvents();
profileEvents();
sandboxEvents();
userEvents();

Note that the events are not passed into the a7.init() function, they are simply included here for availability when the application starts.

Modules

Events are typically organized into modules of related events for convenience. Below is a section of the profile.js event module from LacesIDE.

import { a7 } from '/lib/altseven/dist/a7.js';
import { ui } from '/js/app.ui.js';
//import {main} from '/js/app.main.js';
import * as utils from '/js/app.utils.js';

export var profileEvents = function init() {

	a7.events.subscribe("profile.update", function (obj) {
			a7.remote.invoke("profile.update", obj)
				.then(function (response) {
					// get json response and pass to handler to resolve
					return response.json();
				})
				.then(function (json) {
					if (json.success) {
						utils.showNotice("Profile saved.", "#pTab1Notice");
						a7.events.publish("profile.refreshProfile");
					}else{
						utils.showNotice("Profile not saved.", "#pTab1Notice");
					}
				});
		});
};

In this case, the profile.update event calls the remote method of the same name and updates the picture for the user’s profile. You can see in the profile.js view how that call is implemented. Below is a fragment of that code that shows how to publish an event. You call a7.events.publish( eventname, args ) where args is an object that contains the arguments you want to pass to the event subscription.

		updatePic: function (file) {
			let user = a7.model.get("user");
			user.profilePic = file.path + file.filename;
			a7.events.publish("profile.update", user);
		},

While many of the event subscriptions in the LacesIDE application work with remote methods, it is not necessary to call remote methods in the event. You can look at the sandbox.execute subscription in the sandbox.js module for an example of why you might want to use events aside from interacting with remote methods.

The event system contains a set of pre-defined events for the built-in authentication system. You can see more details about these events in the Authentication tutorial.

    a7.events.subscribe("auth.login", function(params) {
      a7.remote.invoke("auth.login", params);
    });
    a7.events.subscribe("auth.logout", function(params) {
      a7.remote.invoke("auth.logout", params);
    });
    a7.events.subscribe("auth.refresh", function(params) {
      a7.remote.invoke("auth.refresh", params);
    });
    a7.events.subscribe("auth.sessionTimeout", function() {
      a7.security.invalidateSession();
    });
    a7.events.subscribe("auth.invalidateSession", function() {
      a7.security.invalidateSession();
    });