What is the best way to access a Service in a Custom MatTableDatasource class?

Discussão em 'Angular' iniciado por Scieur Arnaud, Dezembro 17, 2024.

  1. So, while doing refactoring, I decided to create a custom MatTableDataSource.

    The reasons for that is simple :


    • I've a lot of Table in my Application and they depend of the same Filter/Sort function


    • I want to use this function as filterPredicate.


    • And I want to write it only once for better maintenance and to minimize humans errors later on.

    export class CustomDatasource extends MatTableDataSource<any>{

    constructor(private Mydata) {
    super();
    this.data = MyData;
    }

    filterPredicate = (data, filter: any) => {
    return this.FilterService.MyHugeFilterFunction(filter, data)
    };

    }


    The problem is :

    • MyHugeFilterFunction is a HUGE Function and it use functions in others services.

    So i can just pass it to my model, neither can i just put it as static without doing the same for the others functions that are called within

    I've thinked about multiples solutions to this problem :

    • I could use a Factory to create the models.
    • I could pass the function and all the one related in static.
    • I could start using Injection to pass the service.
    • ETC.

    But I would like to know what would be considered the best way to solve this problem.

    Continue reading...

Compartilhe esta Página