Skip to content

Instantly share code, notes, and snippets.

@omatrot omatrot/Class.hbs
Last active Sep 27, 2019

Embed
What would you like to do?
Dcerialize Trackable Entities Class template
{{> imports}}
export class {{class}} extends TrackableEntity {
{{{> properties}}}
{{{> constructor}}}
}
{{spaces 2}}constructor() {
{{spaces 4}}super();
{{spaces 4}}return super.proxify(this);
{{spaces 1}} }
{{spaces 2}}static onSerialized(json: IJsonObject, instance: {{class}}) {
{{spaces 4}}// Add ITrackable properties that cannot be decorated
{{spaces 4}}json["trackingState"] = instance.trackingState;
{{spaces 4}}json["modifiedProperties"] = SerializeSet<String>
{{spaces 4}}(
{{spaces 6}}Array.from(instance.modifiedProperties.values()),
{{spaces 6}}() => String
{{spaces 4}});
{{spaces 4}}// Custom processing of dates (May be empty)
{{#each properties}}
{{#ifCond property-type '==' 'Date'}}
{{spaces 4}}if (instance.{{property-name}}) {
{{spaces 6}}json["{{property-name}}"] = instance.{{property-name}}.toISOString();
{{spaces 4}} }
{{/ifCond}}
{{/each}}
{{spaces 2}} }
import { TrackableEntity } from 'trackable-entities';
{{#each imports}}
import { {{import}} } from './{{import}}';
{{/each}}
import {
autoserializeAs,
autoserializeAsArray,
SerializeSet,
IJsonObject
} from "dcerialize";
{{#each properties}}
{{#ifCond property-type '==' 'number'}}
{{spaces 2}}@autoserializeAs(() => Number)
{{else}}
{{/ifCond}}
{{#ifCond property-type '==' 'string'}}
{{spaces 2}}@autoserializeAs(() => String)
{{else}}
{{/ifCond}}
{{#ifCond property-type '==' 'boolean'}}
{{spaces 2}}@autoserializeAs(() => Boolean)
{{else}}
{{/ifCond}}
{{#ifCond property-type '==' 'Date'}}
{{spaces 2}}@autoserializeAs(() => Date)
{{else}}
{{/ifCond}}
{{#ifCond property-type '==' 'any'}}
{{spaces 2}}@autoserializeAs(() => String)
{{else}}
{{/ifCond}}
{{spaces 2}}{{property-name}}: {{#ifCond property-type '==' 'any'}}string{{else}}{{property-type}}{{/ifCond}};
{{/each}}
{{#if nav-properties}}
{{#each nav-properties}}
{{#if nav-property-collection}}
{{spaces 2}}@autoserializeAsArray(() => {{nav-property-type}})
{{spaces 2}}{{nav-property-name}}: {{nav-property-type}}[];
{{else}}
{{spaces 2}}@autoserializeAs(() => {{nav-property-type}})
{{spaces 2}}{{nav-property-name}}: {{nav-property-type}};
{{/if}}
{{/each}}
{{/if}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.