@ima/core
Version:
IMA.js framework for isomorphic javascript application
104 lines (103 loc) • 2.83 kB
JavaScript
import { Controller } from './Controller';
/**
* Basic implementation of the {@link Controller} interface, providing the
* default implementation of the most of the API.
*/ export class AbstractController extends Controller {
_pageStateManager;
_extensions = new Map();
/**
* The HTTP response code to send to the client.
*/ status = 200;
/**
* The route parameters extracted from the current route. This field is
* set externally by IMA right before the {@link Controller#init} or the
* {@link Controller#update} method is called.
*/ params = {};
static $name;
static $dependencies;
static $extensions;
constructor(){
super();
}
/**
* @inheritDoc
*/ setState(statePatch) {
if (this._pageStateManager) {
this._pageStateManager.setState(statePatch);
}
}
/**
* @inheritDoc
*/ getState() {
if (this._pageStateManager) {
return this._pageStateManager.getState();
} else {
return {};
}
}
/**
* @inheritDoc
*/ beginStateTransaction() {
if (this._pageStateManager) {
this._pageStateManager.beginTransaction();
}
}
/**
* @inheritDoc
*/ commitStateTransaction() {
if (this._pageStateManager) {
this._pageStateManager.commitTransaction();
}
}
/**
* @inheritDoc
*/ cancelStateTransaction() {
if (this._pageStateManager) {
this._pageStateManager.cancelTransaction();
}
}
/**
* @inheritDoc
*/ addExtension(extension, extensionInstance) {
// FIXME IMA@20, remove backwards compatibility
if (!extensionInstance && typeof extension !== 'object' || extensionInstance && typeof extensionInstance !== 'object') {
throw new Error(`ima.core.AbstractController:addExtension: Expected instance of an extension, got ${typeof extension}.`);
}
if (extensionInstance) {
this._extensions.set(extension, extensionInstance);
} else {
this._extensions.set(extension?.constructor ?? extension, extension);
}
}
/**
* @inheritDoc
*/ getExtension(extension) {
return this._extensions.get(extension);
}
/**
* @inheritDoc
*/ getExtensions() {
return Array.from(this._extensions.values());
}
/**
* @inheritDoc
*/ setRouteParams(params = {}) {
this.params = params;
}
/**
* @inheritDoc
*/ getRouteParams() {
return this.params;
}
/**
* @inheritDoc
*/ setPageStateManager(pageStateManager) {
this._pageStateManager = pageStateManager;
}
/**
* @inheritDoc
*/ getHttpStatus() {
return this.status;
}
}
//# sourceMappingURL=AbstractController.js.map