2018-07-12 14:11:31 +02:00
|
|
|
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-07-12 14:11:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Define that an ID might be a number or a string.
|
|
|
|
*/
|
|
|
|
export type ModelId = number | string;
|
|
|
|
|
2018-08-24 13:05:03 +02:00
|
|
|
export interface ModelConstructor {
|
|
|
|
new (...args: any[]): BaseModel;
|
|
|
|
}
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* 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-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* 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
|
2018-07-12 14:11:31 +02:00
|
|
|
*/
|
|
|
|
abstract id: ModelId;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* constructor that calls super from parent class
|
|
|
|
*/
|
|
|
|
protected constructor() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2018-08-24 13:05:03 +02:00
|
|
|
public static registerCollectionElement(collectionString: string, type: any) {
|
|
|
|
CollectionStringModelMapperService.registerCollectionElement(collectionString, type);
|
|
|
|
}
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* returns the collectionString.
|
|
|
|
*
|
|
|
|
* The server and the dataStore use it to identify the collection.
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
deserialize(input: any): this {
|
|
|
|
Object.assign(this, input);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|