0

So I wanted to make a question not about code but about choices. As the project I'm working on grows, I get more worried about what are the right approaches to different ideas/patterns.

In this case, based on https://angular.io/docs/ts/latest/guide/server-communication.html and https://stackoverflow.com/a/34450948/6028947

I wanted to know what would be best, more recommended, or if it's just a matter of preference whether to put several GET requests in the same file, or have more files, more component, and so it is more separated.

The idea in my case is, I am going to click something, get answers from back-end, show those answers and choices, click on them, get answers, get shown more choices, and that can be a path of, at least, 4 steps.

So I was wondering wether I would need to do the equivalent to the hero-list.component.ts for every step, as to have several GET and several subscribers, or it is not needed.

Thanks in advance.

Community
  • 1
  • 1
monkey intern
  • 705
  • 3
  • 14
  • 34

1 Answers1

0

Referencing your example from the other thread on SO:

@Injectable()
export class CompanyService {
  constructor(http:Http) {
    this.http = http;
  }

  getCompanies() {
    return this.http.get('https://angular2.apispark.net/v1/companies/')
              .map(res => res.json());
  }
} 

...you would bundle all http requests related to a company in this service, e.g. updating its preferences, uploading an image for the company, deleting it and so on.

If you have the need to update a user profile, you would probably create a UserService and fill it with appropriate methods.

So, if I got your question right: yes, bundle http requests that deal with a particular business resource like a company in the same service.

Jan B.
  • 6,030
  • 5
  • 32
  • 53
  • Mmmm not sure we are talking the same. I believe is the second case you mentioned: I'm going to first get a date, then I'm going to get an id, then I'm going to get some very different numbers... So I thiiiiiink it would be the second case, where I have different needs and so I need to create different services? Would that be right? – monkey intern Jul 13 '16 at 11:45
  • Hmm, I'm not sure either whether we mean the same. :-/ Whatever the type of data is (id, date, number, image) you expect to receive from the server, as long as it is related to the company, put it into the same CompanyService. – Jan B. Jul 13 '16 at 12:03