OpenSlides/client/src/app/shared/models/base.model.ts

59 lines
1.6 KiB
TypeScript
Raw Normal View History

import { OpenSlidesComponent } from 'app/openslides.component';
2018-08-24 13:05:03 +02:00
import { Deserializable } from './deserializable.model';
import { CollectionStringModelMapperService } from '../../core/services/collectionStringModelMapper.service';
2018-08-24 13:05:03 +02:00
export interface ModelConstructor {
new (...args: any[]): BaseModel;
}
/**
* Abstract parent class to set rules and functions for all models.
*/
2018-08-24 13:05:03 +02:00
export abstract class BaseModel extends OpenSlidesComponent implements Deserializable {
2018-08-29 13:21:25 +02:00
/**
* Register the collection string to the type.
* @param collectionString
* @param type
*/
public static registerCollectionElement(collectionString: string, type: any) {
CollectionStringModelMapperService.registerCollectionElement(collectionString, type);
}
/**
* force children of BaseModel to have a collectionString.
*
* Has a getter but no setter.
*/
protected abstract _collectionString: string;
/**
2018-08-24 13:05:03 +02:00
* force children of BaseModel to have an id
*/
public abstract id: number;
/**
* constructor that calls super from parent class
*/
protected constructor() {
super();
}
/**
* returns the collectionString.
*
* The server and the dataStore use it to identify the collection.
*/
2018-08-29 13:21:25 +02:00
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.
*/
2018-09-04 11:33:28 +02:00
public deserialize(input: any): void {
Object.assign(this, input);
}
}