I am trying to create a "config" page of sort, and I have a few areas where a user will "add" an object/device. This opens a Dialog window, with that "object's" properties/fields. There is also a Save and Cancel button. When the user hits Save, it works and adds it to the array/list, and displays all of the added objects as such. Within the component for each array item, I also have an edit button. This Edit button brings up the same dialog window. Here my goal is for the user to "edit" the data, without making a real time change to the list object, until "save" is pressed.
I have this working if I just pass a single property back and forth (IE object.name), Since I want to re-use this logic, I want to pass the object as a whole, and by pass I mean to essentially make a copy of the original, when they hit edit button, make "edits" to the copy in the dialog, then when they hit save. it takes the "copy" and passes it back to the original, thus changing the object in the list.
Like I said, Adding to the list isn't a problem, it is "editing" where I am stuck. I will also add, I am fairly new to the world of angular and javascript/typescript, but have experience in some OOP (Object-Oriented)
I have this working once again If I just do a single property (First bit of code)
//This works as RoomGroup only has a name property.
export class RoomGroupDialogComponent implements OnInit {
tempGroup: ConfigRoomGroup;
newGroup = true;
constructor(public diaglogRef: MatDialogRef<RoomGroupDialogComponent>,
@Inject(MAT_DIALOG_DATA) private group: ConfigRoomGroup,
public config: ConfigService) { }
ngOnInit() {
if (this.group.name) {
this.newGroup = false;
} else {
this.newGroup = true;
this.group.name = 'New Group';
}
this.tempGroup = Object.assign({}, this.group);
}
onSaveClick(): void {
if (this.newGroup) {
this.config.configData.roomSetup.groups.push(this.tempGroup);
} else {
this.group.name = this.tempGroup.name;
console.log(this.group);
}
this.diaglogRef.close();
}
onCancelClick(): void {
this.diaglogRef.close();
}
}
///This does not work
export class RoomDialogComponent implements OnInit {
tempRoom: ConfigRoom;
newRoom = true;
constructor(public dialogRef: MatDialogRef<RoomDialogComponent>,
@Inject(MAT_DIALOG_DATA) private room: ConfigRoom,
public config: ConfigService) { }
ngOnInit() {
if ( this.room.name) {
this.newRoom = false;
} else {
this.newRoom = true;
this.room.name = 'New Room';
}
this.tempRoom = Object.assign({}, this.room);
}
onSaveClick(): void {
if (this.newRoom) {
console.log(this.tempRoom);
this.config.configData.roomSetup.rooms.push(this.tempRoom);
} else {
this.room = this.tempRoom;
console.log(this.tempRoom);
console.log(this.room);
console.log(this.config.configData.roomSetup.rooms);
}
this.dialogRef.close();
}
onCancelClick(): void {
this.dialogRef.close();
}
}