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
|
|
|
|
2018-09-10 08:57:53 +02:00
|
|
|
export interface ModelConstructor<T extends BaseModel> {
|
|
|
|
new (...args: any[]): T;
|
2018-08-24 13:05:03 +02:00
|
|
|
}
|
|
|
|
|
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-08-29 13:21:25 +02:00
|
|
|
/**
|
|
|
|
* Register the collection string to the type.
|
|
|
|
* @param collectionString
|
|
|
|
* @param type
|
|
|
|
*/
|
2018-09-07 13:12:59 +02:00
|
|
|
public static registerCollectionElement(collectionString: string, type: any): void {
|
2018-08-29 13:21:25 +02:00
|
|
|
CollectionStringModelMapperService.registerCollectionElement(collectionString, type);
|
|
|
|
}
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* force children of BaseModel to have a collectionString.
|
|
|
|
*
|
|
|
|
* Has a getter but no setter.
|
|
|
|
*/
|
2018-09-10 11:15:12 +02:00
|
|
|
protected _collectionString: string;
|
2018-07-12 14:11:31 +02:00
|
|
|
|
|
|
|
/**
|
2018-08-24 13:05:03 +02:00
|
|
|
* force children of BaseModel to have an id
|
2018-07-12 14:11:31 +02:00
|
|
|
*/
|
2018-09-07 12:51:16 +02:00
|
|
|
public abstract id: number;
|
2018-07-12 14:11:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* constructor that calls super from parent class
|
|
|
|
*/
|
2018-09-10 11:15:12 +02:00
|
|
|
protected constructor(collectionString: string, input?: any) {
|
2018-07-12 14:11:31 +02:00
|
|
|
super();
|
2018-09-10 11:15:12 +02:00
|
|
|
this._collectionString = collectionString;
|
|
|
|
|
|
|
|
if (input) {
|
2018-09-04 14:55:07 +02:00
|
|
|
this.changeNullValuesToUndef(input);
|
2018-09-10 11:15:12 +02:00
|
|
|
this.deserialize(input);
|
|
|
|
}
|
2018-07-12 14:11:31 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 14:55:07 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* 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 {
|
2018-07-12 14:11:31 +02:00
|
|
|
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 {
|
2018-07-12 14:11:31 +02:00
|
|
|
Object.assign(this, input);
|
|
|
|
}
|
|
|
|
}
|