OpenSlides/client/src/app/shared/models/base.model.ts
Sean Engelhardt 39f266d2de Motion Repository
Adds a repository to remove the logic in the motion domain object
The repository will add a new model layer between the components
and the domain objects

implicitly add a new buttion into the motion detail view
2018-09-10 14:53:47 +02:00

77 lines
2.2 KiB
TypeScript

import { OpenSlidesComponent } from 'app/openslides.component';
import { Deserializable } from './deserializable.model';
import { CollectionStringModelMapperService } from '../../core/services/collectionStringModelMapper.service';
export interface ModelConstructor<T extends BaseModel> {
new (...args: any[]): T;
}
/**
* Abstract parent class to set rules and functions for all models.
*/
export abstract class BaseModel extends OpenSlidesComponent implements Deserializable {
/**
* Register the collection string to the type.
* @param collectionString
* @param type
*/
public static registerCollectionElement(collectionString: string, type: any): void {
CollectionStringModelMapperService.registerCollectionElement(collectionString, type);
}
/**
* force children of BaseModel to have a collectionString.
*
* Has a getter but no setter.
*/
protected _collectionString: string;
/**
* force children of BaseModel to have an id
*/
public abstract id: number;
/**
* constructor that calls super from parent class
*/
protected constructor(collectionString: string, input?: any) {
super();
this._collectionString = collectionString;
if (input) {
this.changeNullValuesToUndef(input);
this.deserialize(input);
}
}
/**
* Prevent to send literally "null" if should be send
* @param input object to deserialize
*/
public changeNullValuesToUndef(input: any): void {
Object.keys(input).forEach(key => {
if (input[key] === null) {
input[key] = undefined;
}
});
}
/**
* returns the collectionString.
*
* The server and the dataStore use it to identify the collection.
*/
public get collectionString(): string {
return this._collectionString;
}
/**
* Most simple and most commonly used deserialize function.
* Inherited to children, can be overwritten for special use cases
* @param input JSON data for deserialization.
*/
public deserialize(input: any): void {
Object.assign(this, input);
}
}