Picture a study tool with flashcards and different categories. Each category has a title, while each card contains content, is linked to only one category, and is also connected to another card.
How can I establish these connections in Angular 5 and Typescript without relying on external services? By connections, I mean how can the class independently determine its associated elements?
/* card.ts */
export class Card {
public id: String;
public content: String;
public link: Card;
public category: Category;
constructor(
content, category
) {
this.content = content;
this.category = category;
this.generateId();
}
private generateId(): void {
this.id = /* generate unique id */;
}
public setLink(card) {
this.link = card;
}
}
/* category.ts */
import { Card } from './card';
export class Category {
public title: String;
public slug: String;
constructor (
title
) {
this.title = title;
this.generateSlug();
this.generateId();
}
private generateId(): void {
this.id = /* generate unique id */;
}
public getCards: Card[] {
/* I NEED ASSISTANCE HERE */
/* perform magic to retrieve all cards related to this category */
}
}
I have indicated within the code (I NEED ASSISTANCE HERE
) the point where a category should be able to identify all corresponding cards within that specific category. In the future, I aim to simply call Category.getCards()
to access all cards linked to the chosen category.
Previously, I attempted to create service providers to scan through all cards and locate the appropriate ones, but I encountered difficulties injecting these services into my class for utilization.
PS: My background lies in PHP and Laravel, which provides seamless connections between classes using various relationships.