2.7.0 • Published 3 months ago

modapp v2.7.0

Weekly downloads
4
License
MIT
Repository
github
Last release
3 months ago

view on npm

ModApp

A thin framework for building modular web applications.

Installation

With npm:

npm install modapp

With yarn:

yarn add modapp

Example

import { App } from 'modapp';

// A module keeping track of greetings
class Greetings {
	constructor(app, params) {
		this.app = app;
		this.greetings = {};
	}

	addGreeting(lang, greeting) {
		this.greetings[lang] = greeting;
	}

	removeGreeting(lang) {
		delete this.greetlings[lang];
	}

	greet(lang) {
		let greeting = this.greetings[lang];
		console.log(greeting || "Unknown language");
	}

	dispose() {} // Nothing to dispose
}

// A module for swedish greetings
class EnglishGreeting {
	constructor(app, params) {
		this.app = app;
		this.greeting = params.greeting || "Hello";
		this.app.require(['greetings'], this._init.bind(this));
	}

	_init(module)  {
		this.module = module;
		this.module.greetings.addGreeting('english', this.greeting);
	}

	dispose() {
		this.module.greetings.removeGreeting('english');
	}
}

// Creating the app with some module configuration. These parameters may be overwritten using url queries
let app = new App({ englishGreeting: { greeting: "Hi" }});

// Bundles are usually created dynamically using tools like webpacks require context.
let bundle = {
	greetings: Greetings,
	englishGreeting: EnglishGreeting
};

app.loadBundle(bundle)
	.then(result => {
		console.log("Modules loaded: ", result);
		app.getModule('greetings').greet('english');
	});

Using modapp

Modules can be loaded, deactivate, and reactivated while the application is running, allowing hotloading of new features and functionality to the application. New modules will simply hook into the application using the register/unregister methods provided (addGreeting and removeGreeting in the example).

Try resgate-test-app to see modapp in use.

AppModule interface

For a class to be loaded by the app, it must follow the AppModule interface.
A module may expose any number of public methods, using camelCase as naming convention.
All properties should be considered private, as well as any methods starting with an underscore (_).
Any exposed method that allows other modules to register objects or callbacks, must come paired with a method for unregistering.

appModule.constructor(app, params)

The app module constructor.
It is only in the constructor that the module may call app.require(...). If app.require is called, the module must postpone registering any objects or callbacks until the require callback is called to prevent memory leaks in case the App cannot fulfill the requirements and discards the app module. (#AppModule)

ParamTypeDescription
appAppThe app instance
paramsObject.<string, *>Module parameters from the app config

appModule.dispose()

Disposes the app module, removing any items or callbacks previously registered within the constructor or in the app.require callback.
Once disposed, no more calls will be done to the module instance.

Documentation

Markdown documentation

2.7.0

3 months ago

2.6.2

6 months ago

2.6.1

10 months ago

2.6.0

2 years ago

2.5.0

3 years ago

2.4.0

4 years ago

2.3.3

6 years ago

2.3.2

6 years ago

2.3.1

6 years ago

2.3.0

6 years ago

2.2.0

6 years ago

2.1.3

6 years ago

2.1.2

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.3

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago