2

I am developing an Angular 2 application where I have some services (Angular 2 service) that communicates to my server side methods through HTTP calls. I am calling the services in components to get the data and display them on the views. This is working fine but I have written separate services for separate data operations. For example I have a Dashboard where I am loading a list of tasks, so for that I have a loadTask method in my component as follows --

public loadTask() {

        console.log('Component : Dashboard Component - Method : loadTask');
        //Get the current user
        let currentUser = JSON.parse(localStorage.getItem('currentUser'));
        this.taskListBusy = this._dashboardService.getTaskList(currentUser.EmployeeId).subscribe(
            data => this.taskListItems = data,
            error => this._errorLoggerService.logError(error, 'Dashboard/TaskList Component'),
            () => console.log("Task List For User Id - " + this._userLoginService.getCurrentUserId() + "\n" + JSON.stringify(this.taskListItems)));
    }

This method is calling a service method i.e.

this._dashboardService.getTaskList(currentUser.EmployeeId)

where _dashboardService.getTaskList() is a service method as follows --

public getTaskList(userId: any): Observable<TaskListModel[]> {
        console.log('Service - DashboardService : Method - getTaskList : Params - ' + userId);
        let postData = "userId=" + userId;

        this._requestOptions = new RequestOptions({
            method: RequestMethod.Post,
            url: this._httpHelperService.apiUrl + 'Dashboard/getTaskListByUserId',
            headers: this._httpHelperService.contentTypeJson,
            body: JSON.stringify({ 'userId': userId })
        });

        console.log('Get Task List - POST Request Query : ' + JSON.stringify(this._requestOptions));
        return this._http.request(new Request(this._requestOptions)).map(this._httpHelperService.extractData).catch(this._httpHelperService.handleError);
    }

As you can see it is doing nothing but making a http call to get the data. All I want is a generic service that will do all my HTTP calls. It should be such that I will pass the controller name, method name and parameters and then it should make the http call to get the data. Is there a way I can do this ?

6
  • 1
    It sounds like a really weird solution. Why would you rather have a generic service that takes the component (not controller) and method name instead of just having meaningful names on the separate service calls - such as the one you have: getTaskList? Commented Jun 20, 2017 at 13:25
  • @FredrikLundin - Do you mean what I am doing currently is a correct approach ? Commented Jun 20, 2017 at 16:33
  • Yes, that's the most common way of doing things in angular. Create a service for your component and make the http requests from there 👍 Commented Jun 20, 2017 at 16:36
  • @FredrikLundin - Can you please give me a reason why it won't be good to have a generic function that will all my http calls ? and how creating separate services for separate components is a good idea. I am going by the way you suggested but just for the sake of knowledge, I want to know this. Commented Jun 27, 2017 at 12:40
  • Well, it all depends on your use-case obviously. But in general - the http service from @angular/http is already a generic service for doing http. But if you need to wrap it for all your calls, then go ahead. Usually you want to do different things for different data, and the http service is usually abstracted enough Commented Jun 27, 2017 at 12:44

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.