// 'view' for viewing an existing record without editing
@Input() mode: string;
- // record ID to update. Not all IDs are numbers.
- @Input() recordId: string;
+ // Record ID to view/update. Value is dynamic.
+ recId: any;
+ @Input() set recordId(id: any) {
+ if (id) this.recId = id;
+ }
// TODO
// customFieldTemplates
idlDef: any;
// IDL record we are editing
+ // TODO: allow this to be provided by the caller?
record: EgIdlObject;
// Can we edit the primary key?
private initRecord(): Promise<any> {
if (this.mode == 'update' || this.mode == 'view') {
- return this.pcrud.retrieve(this.idlClass, this.recordId)
+ return this.pcrud.retrieve(this.idlClass, this.recId)
.toPromise().then(rec => {
if (!rec) {
- return Promise.reject(
- `No record found with id ${this.recordId}`);
+ return Promise.reject(`No '${this.idlClass}'
+ record found with id ${this.recId}`);
}
this.record = rec;
this.convertDatatypesToJs();
- return this.getFieldList().then(
- fields => this.fields = fields);
+ return this.applyFields();
});
}
// create a new record from scratch
this.pkeyIsEditable = !('pkey_sequence' in this.idlDef);
this.record = this.idl.create(this.idlClass);
- return this.getFieldList().then(fields => this.fields = fields);
+ return this.applyFields();
}
// Modifies the FM record in place, replacing IDL-compatible values
let idField = this.idl.classes[cls].pkey;
let selector =
this.idl.classes[cls].field_map[idField].selector || idField;
+
return list.map(item => {
return {id: item[idField](), name: item[selector]()}
});
}
- private getFieldList(): Promise<any[]> {
+ private applyFields(): Promise<any> {
- let fields = this.idlDef.fields.filter(f =>
+ this.fields = this.idlDef.fields.filter(f =>
f.virtual != 'true' &&
!this.hiddenFields.includes(f.name)
);
let promises = [];
- fields.forEach(field => {
+ this.fields.forEach(field => {
field.readOnly = this.mode == 'view'
|| this.readonlyFields.includes(field.name);
// TODO custom field templates
});
- return Promise.all(promises).then(ok => fields);
+ // Wait for all network calls to complete
+ return Promise.all(promises);
}
save() {