How do you generate a single file containing the parent class and all child classes? - typewriter

I've currently got a tst that generates the parent class, but the child classes need to be listed separately in the file as well so that I end up with 1 file that contains all necessary files for the front end.
key parts of the tst look like this at the moment:
$Imports
$Classes([TsType])[
export interface $Name $Extends {
$Properties(o=>o.Attributes.Any(a=>a.Name=="TsIgnore")==false)[
$name$IfOptional: $TypeNamespace$Type;]
}]
$Classes(*Generic)[
class $Name$TypeParameters {
$Properties[
public $name: $Type;]
}]
$Enums([TsType])[
export enum $Name{
$Values[
$name,
]
}
]

In the $Classes you can access the $NestedClasses. See here: http://frhagn.github.io/Typewriter/pages/documentation.html
$Classes(*Generic)[
class $Name$TypeParameters {
$Properties[
public $name: $Type;]
$NestedClasses(*Generic)[
class $Name$TypeParameters {
$Properties[
public $name: $Type;]
}]
}]

Related

Is there a way to create one array properties with many mongoDB objectiID inside

I use Loopback4.
When i want to add a mongoDb ObjectId property inside my model i do that :
#property({
type: 'string',
mongodb: {dataType: 'ObjectID'},
})
organizationId?: string;
Now i want to make an array with MongoDB ObjectId properties inside, so i tried to do :
#property({
type: 'array',
itemType: 'string',
mongodb: {dataType: 'ObjectID'},
})
tagsId?: string[];
but it seems like all the array is converted to one ObjectID inside the mongoDb.
What I want to do is to simply get an array with many ObjectId inside. I tried everything in my knowledgme: that was not enough.
i found a solution :
Step 1 :
create a model with just one id.
Step 2 :
Make an array with your new model
Step 1 :
In your future model (in my case : tagReference) :
#model()
export class TagReference extends Entity {
#property({
type: 'string',
mongodb: {dataType: 'ObjectID'},
})
id?: string;
constructor(data?: Partial<TagReference>) {
super(data);
}
}
Step 2:
Where you want your array :
import {TagReference} from './tag-reference.model';
#model()
export class Resource extends BaseEntity {
// ...
#property({
type: 'array',
itemType: TagReference,
})
tagIds?: string[];
// ...
}

Reading an array from another Class

I have a Class on a Theme.swift called Themes file which basically defines some types of Strings, and set different colours and font types.
On another file, called Contents.swift I have another class called Contents, and some arrays, like this:
class Contents: Themes {
let navContent = [
LabelContent(text: "NAV IDENT", theme: .menuOption),
LabelContent(text: "WPT LIST", theme: .menuOption)
]
}
The question is:
How can I use this navContent array in AppDelegate? It's not global yet I think.
If you don't want to create an instance of the class Contents and then get its properties, then make those properties static (or Type Properties):
class Contents: Themes {
static let navContent = [
LabelContent(text: "NAV IDENT", theme: .menuOption),
LabelContent(text: "WPT LIST", theme: .menuOption)
]
}
And you could access them like so:
print(Contents.navContent[0])
Here is a brief description of Type Properties from the documentation:
You can define properties that belong to the type itself, not to any one instance of that type. There will only ever be one copy of these properties, no matter how many instances of that type you create. These kinds of properties are called type properties.
if it's a member variable of the Contents class as you have it there, you need to create an instance of Contents, then access it from the instance
let instance = Contents()
instance.navContent // access it this way
if you dont want to create an instance everytime you need to access it, then you can make it into static class variable
class Contents: Themes {
static let navContent = [
LabelContent(text: "NAV IDENT", theme: .menuOption),
LabelContent(text: "WPT LIST", theme: .menuOption)
]
}
Contents.navContent // access it this way
if you need polymorphism on that variable, you can use the 'class' keyword on the variable and you can override it on the child class (but it needs to be a computed variable). you access it the same way as static class variable
class Contents: Themes {
class var navContent: [LabelContent] {
return [LabelContent(text: "NAV IDENT", theme: .menuOption)]
}
}
class OtherContents: Contents {
override class var navContent: [LabelContent] {
return [LabelContent(text: "WPT LIST", theme: .menuOption)]
}
}
Contents.navContent // access "NAV IDENT"
OtherContents.navContent // access "WPT LIST"

PrimeNG multiselect not binding in Model Driven form when pulling "typed" data from server

I am using PrimeNG Multiselect in a reactive Angular form.
I can see the array of values coming back from the server through the chrome dev tool, but I cant figure out how to get it to bind to the multi select.
module is imported in the component and the app module:
import { MultiSelectModule } from 'primeng/multiselect';
template:
<p-multiSelect [options]="activities" [optionLabel]="ActivityName" [panelStyle]="{minWidth: '300px'}" [showHeader]="true" formControlName="selectedActivities"></p-multiSelect>
Class:
export class ChemicalEditComponent implements OnInit {
activities: ActivityType[];
selectedActivities: ActivityType[];
constructor(
private _chemicalService: ChemicalService,
private fb: FormBuilder
) {
this.getActivities();
this.getSelectedActivities();
this.createForm();
}
ngOnInit() {
this.getChemical();
}
getActivities(): void {
this._chemicalService.getActivities().subscribe(result => this.activities = result);
}
getSelectedActivities(): void {
this._chemicalService.getActivitiesByChemical(this.chemicalId).subscribe(result => this.selectedActivities = result);
}
createForm() {
this.chemicalForm = this.fb.group({
brandName: '',
commonName: '',
formulation: '',
selectedActivities: ''
});
}
rebuildForm() {
this.chemicalForm.reset({
brandName: this.chemical.BrandName,
commonName: this.chemical.CommonName,
formulation: this.chemical.Formulation,
selectedActivities: this.selectedActivities
});
}
The json returned from the server looks like this:
0:{ActivityTypeId: 61, ActivityName: "Driving"}
1:{ActivityTypeId: 62, ActivityName: "Strolling"}
I'm seeing all the examples use "label" and "value" but mine is typed from the server. I'm assuming the problem is I need to specify what is the label & value, so I added the [optionLabel] tag but that didnt help.

how to represent state object as typescript interface

I thought I am comfortable with Javascript and React, but currently suffering through typescript learning curve. I have my react state defined as:
state = {
fields: { // list of fields
symbol: '',
qty: '',
side: ''
},
fieldErrors: {}
};
I want to be able to use it as following (dictionary):
onInputChange = (name :string, value :string, error :string) => {
const fields = this.state.fields;
const fieldErrors = this.state.fieldErrors;
fields[name] = value;
fieldErrors[name] = error;
this.setState({fields, fieldErrors});
}
How do I represent my state in terms of Typescript? I am trying something like:
interface IFields {
name: string
}
interface IOrderEntryState {
fields: IFields,
fieldErrors: IFields
}
Pardon if my question sounds illiterate, totally new at this. Thanks
Based on your snippet, it looks like fields[name] is assigning an arbitrary key to that object. So you probably want to use an index signature to represent that instead of the hardcoded key name as you have now.
So your interfaces probably should look more like this:
interface IFields {
// This is an index signature. It means this object can hold
// a key of any name, and they can be accessed and set using
// bracket notation: `this.state.fields["somekey"]`
[name: string]: string
}
interface IOrderEntryState {
fields: IFields,
fieldErrors: IFields
}
If you want a dictionary you can declare one using generic type with index property. It would look like this:
interface Dictionary<TKey, TVal> {
[key: TKey]: TVal;
}
interface IOrderEntryState {
fields: Dictionary<string, string>,
fieldErrors: Dictionary<string, string>
}
This makes IOrderEntryState.fields have arbitrary string attribute names with string values.

Grails 3.1 JSON-Views, rendering a Category Tree

I'm using Graisl 3.1.1, rest-api profile.
I'm trying to build a Category Tree but I haven some problems rendering the categories in JSON-Views.
I'm using json templates, one for the parent and another for the child.
Basically I want to generate a json for angular something like this:
These is my code.
Any help?
//domain
class Category {
ObjectId id /* MongoDB */
static hasMany = [categories: Category]
String name
...
//controller
def directory(){
def categories = Category.findAllByCategoriesIsNotNull([sort: 'name', order: 'asc'])
respond categories
}
//directory.gson
import com.example.Category
model {
Iterable<Category> categoryList
}
json {
categories g.render(template: 'parent', collection: categoryList ?: [], var: 'category')
}
//_parent.gson
import com.example.Category
model {
Category category
}
json {
id category.id.toString()
name category.name
categories g.render(template: "category/child", collection: category.categories ?: [], var: 'child')
}
The problem is the categories line above, I'm not sure what is the problem or my mistake.
//_child.gson
import com.example.Category
model {
Category child
}
json {
name child.name
}
I'm reasonably confident you are encountering the same fresh bug (fixed in grails-views#1.0.4) that I encountered a couple weeks ago, #9720:
it would appear that the relative path isn't recognized for g.render and the fully qualified path is required even when the template is located in the same directory as the calling gson file.
Instead of:
categories g.render(template: "category/child", collection: category.categories ?: [], var: 'child')
prepend a slash to the template:
categories g.render(template: "/category/child", collection: category.categories ?: [], var: 'child')
You may also need to change:
categories g.render(template: 'parent', collection: categoryList ?: [], var: 'category')
to:
categories g.render(template: '/category/parent', collection: categoryList ?: [], var: 'category')
I usually use in controller
import grails.converters.JSON
and then
render result as JSON

Resources