Na studiach zaczęliśmy, ćwiczenka z Angular. I mam drobny problem, albo jestem ślepy albo coś nie kminię jakiegoś mechanizmu
updateComment(emitData: NgForm): void {
let index = this.post.comments.findIndex(item => item.id === emitData.value.id);
let foundedEntity = this.post.comments[index];
foundedEntity.nameAutor = emitData.value.autor;
foundedEntity.publishedDate = emitData.value.publishedDate;
foundedEntity.textComment = emitData.value.message;
this.post.comments[index] = foundedEntity;
console.log(this.post.comments[index]) // Update object
console.log(this.post.comments[0]) // tu tez
console.table(this.post) /// A tu nie ma...
this.config.edit = false;
Cały kod:
https://github.com/PatrykMaternicki/BIU/tree/master/Zad.3/src/app
Co najlepsze jak mu wrzucałem cale obiekty konstruowane przez inny komponent to twierdził, że wszystko jest w porządku, ale tamta opcja odpada gdyż:
export class AutorComment {
static counter : number = 0;
constructor(
public nameAutor:string,
public publishedDate: Date,
public textComment: string{},
public subComments: Array<AutorComment>=[]){
this.id = AutorComment.counter++;
}
}
Potrzebuje metody która będzie podnosiła id. Owszem mogłem to zrobić to tak jak tu.
https://github.com/PatrykMaternicki/BIU/blob/11a85373a9c0885b113f035220dcae66868e106c/Zad.3/src/app/blog-post/blog-post.component.ts
Jest to brzydkie rozwiązanie, gdyż użależniam tą klasę (ona tylko może nadawać id)
Kombinowalem jeszcze z metodą static increase, ale jak w TS konstruktorze zainicjonować domyślną wartość. Nie mogłem znależć dla konstruktora dla pola owszem.
Przy okazji ma ktoś pomysł zbudować licznik dla każdej subListy komentarzyy by każdy licznik byłby unikatowy (chyba, że to owrapować kolejnym obiektem), chodzi mi o to że np:
1. komentarz
1.subkomentarz
2.subkomentarz
2.komentarz
1.subkomentarz
oraz macie jak zrobić by nie przekazywać w głąb drzewa parentId (potrzebny przy edycji)
W razie byłby problem powiedzcie jakie jeszcze trzeba pliki udostępnić bo nie robiłem tego nigdy na angualar. W razie potrzeby jestem w stanie zbudować i udostępnić gdzieś wersje live.
Z góry dzięki za odpowiedzi.