Angular - Using Observables and subscribing to its changes [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite
1












I have a component and a service. The service is contacting an API and gets a data which should be handled in the component. Now, I can do it in two ways:



  1. Create a public Observable in my service and the component is subscribed to it and handle changes

  2. All methods in my service return Observables and the component subscribes to every call and then handles the changes.

Let me show a minimal example.



So this is the first solution:



my-service.service.ts



@Injectable()
export class MyService

requestObservable: Observable<any>;
private requestObserver: Observer<any>;

constructor(private http: Http)

this.requestObservable = new Observable<any>(observer =>
this.requestObserver = observer;
);



get(id: string): void
this.http
.get(`/some/url/$id`)
.map(response => response.json())
.subscribe(
next => this.requestObserver.next(next)
);





my-component.component.ts



@Component(...)
export class MyComponent implements OnInit

constructor(private myService: MyService)

this.myService.requestObservable.subscribe(
next => this.handleChanges(next)
);



ngOnInit(): void

this.myService.get(1);
this.myService.get(2);



handleChanges(changes: any): void

console.log(changes);









And this is the same situation but solved in the second way:



my-service.service.ts



@Injectable()
export class MyService

constructor(private http: Http)

get(id: string): Observable<any>
return this.http
.get(`/some/url/$id`)
.map(response => response.json());





my-component.component.ts



@Component(...)
export class MyComponent implements OnInit

constructor(private myService: MyService)

ngOnInit(): void

this.myService.get(1).subscribe(
next =>
console.log(next);

);

this.myService.get(2).subscribe(
next =>
console.log(next);

);







So as you can see both examples are doing the same thing but in other ways. I have omitted unnecessary imports and other things because they are not connected with my problem.



I would say that the first solution is much cleaner since it gives me a possibility to have multiple calls to the same get() method and have one method which handles all of them.



On the other hand, if I had more different methods in my service and every one of them would have to use different Observable then it would make my service more complicated and low-readable.



Please advice which solution is better?







share|improve this question













closed as off-topic by jonrsharpe, Sam Onela, Mast, t3chb0t, Stephen Rauch Apr 7 at 21:54


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Mast, t3chb0t, Stephen Rauch
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1




    As you note, there are different contexts, so there is no "better" - the crucial benefit of the first version is that other components (or services) could also be receiving the updated data, whereas in the second version they'd have to make their own calls (and trigger their own requests). See e.g. my answer stackoverflow.com/a/41554338/3001761 for a neater way to implement it.
    – jonrsharpe
    Apr 7 at 17:52






  • 1




    "Let me show a minimal example." Nonononono, we don't do MCVE, that's Stack Overflow. Please share your actual code and take a look at the help center.
    – Mast
    Apr 7 at 19:10
















up vote
1
down vote

favorite
1












I have a component and a service. The service is contacting an API and gets a data which should be handled in the component. Now, I can do it in two ways:



  1. Create a public Observable in my service and the component is subscribed to it and handle changes

  2. All methods in my service return Observables and the component subscribes to every call and then handles the changes.

Let me show a minimal example.



So this is the first solution:



my-service.service.ts



@Injectable()
export class MyService

requestObservable: Observable<any>;
private requestObserver: Observer<any>;

constructor(private http: Http)

this.requestObservable = new Observable<any>(observer =>
this.requestObserver = observer;
);



get(id: string): void
this.http
.get(`/some/url/$id`)
.map(response => response.json())
.subscribe(
next => this.requestObserver.next(next)
);





my-component.component.ts



@Component(...)
export class MyComponent implements OnInit

constructor(private myService: MyService)

this.myService.requestObservable.subscribe(
next => this.handleChanges(next)
);



ngOnInit(): void

this.myService.get(1);
this.myService.get(2);



handleChanges(changes: any): void

console.log(changes);









And this is the same situation but solved in the second way:



my-service.service.ts



@Injectable()
export class MyService

constructor(private http: Http)

get(id: string): Observable<any>
return this.http
.get(`/some/url/$id`)
.map(response => response.json());





my-component.component.ts



@Component(...)
export class MyComponent implements OnInit

constructor(private myService: MyService)

ngOnInit(): void

this.myService.get(1).subscribe(
next =>
console.log(next);

);

this.myService.get(2).subscribe(
next =>
console.log(next);

);







So as you can see both examples are doing the same thing but in other ways. I have omitted unnecessary imports and other things because they are not connected with my problem.



I would say that the first solution is much cleaner since it gives me a possibility to have multiple calls to the same get() method and have one method which handles all of them.



On the other hand, if I had more different methods in my service and every one of them would have to use different Observable then it would make my service more complicated and low-readable.



Please advice which solution is better?







share|improve this question













closed as off-topic by jonrsharpe, Sam Onela, Mast, t3chb0t, Stephen Rauch Apr 7 at 21:54


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Mast, t3chb0t, Stephen Rauch
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1




    As you note, there are different contexts, so there is no "better" - the crucial benefit of the first version is that other components (or services) could also be receiving the updated data, whereas in the second version they'd have to make their own calls (and trigger their own requests). See e.g. my answer stackoverflow.com/a/41554338/3001761 for a neater way to implement it.
    – jonrsharpe
    Apr 7 at 17:52






  • 1




    "Let me show a minimal example." Nonononono, we don't do MCVE, that's Stack Overflow. Please share your actual code and take a look at the help center.
    – Mast
    Apr 7 at 19:10












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I have a component and a service. The service is contacting an API and gets a data which should be handled in the component. Now, I can do it in two ways:



  1. Create a public Observable in my service and the component is subscribed to it and handle changes

  2. All methods in my service return Observables and the component subscribes to every call and then handles the changes.

Let me show a minimal example.



So this is the first solution:



my-service.service.ts



@Injectable()
export class MyService

requestObservable: Observable<any>;
private requestObserver: Observer<any>;

constructor(private http: Http)

this.requestObservable = new Observable<any>(observer =>
this.requestObserver = observer;
);



get(id: string): void
this.http
.get(`/some/url/$id`)
.map(response => response.json())
.subscribe(
next => this.requestObserver.next(next)
);





my-component.component.ts



@Component(...)
export class MyComponent implements OnInit

constructor(private myService: MyService)

this.myService.requestObservable.subscribe(
next => this.handleChanges(next)
);



ngOnInit(): void

this.myService.get(1);
this.myService.get(2);



handleChanges(changes: any): void

console.log(changes);









And this is the same situation but solved in the second way:



my-service.service.ts



@Injectable()
export class MyService

constructor(private http: Http)

get(id: string): Observable<any>
return this.http
.get(`/some/url/$id`)
.map(response => response.json());





my-component.component.ts



@Component(...)
export class MyComponent implements OnInit

constructor(private myService: MyService)

ngOnInit(): void

this.myService.get(1).subscribe(
next =>
console.log(next);

);

this.myService.get(2).subscribe(
next =>
console.log(next);

);







So as you can see both examples are doing the same thing but in other ways. I have omitted unnecessary imports and other things because they are not connected with my problem.



I would say that the first solution is much cleaner since it gives me a possibility to have multiple calls to the same get() method and have one method which handles all of them.



On the other hand, if I had more different methods in my service and every one of them would have to use different Observable then it would make my service more complicated and low-readable.



Please advice which solution is better?







share|improve this question













I have a component and a service. The service is contacting an API and gets a data which should be handled in the component. Now, I can do it in two ways:



  1. Create a public Observable in my service and the component is subscribed to it and handle changes

  2. All methods in my service return Observables and the component subscribes to every call and then handles the changes.

Let me show a minimal example.



So this is the first solution:



my-service.service.ts



@Injectable()
export class MyService

requestObservable: Observable<any>;
private requestObserver: Observer<any>;

constructor(private http: Http)

this.requestObservable = new Observable<any>(observer =>
this.requestObserver = observer;
);



get(id: string): void
this.http
.get(`/some/url/$id`)
.map(response => response.json())
.subscribe(
next => this.requestObserver.next(next)
);





my-component.component.ts



@Component(...)
export class MyComponent implements OnInit

constructor(private myService: MyService)

this.myService.requestObservable.subscribe(
next => this.handleChanges(next)
);



ngOnInit(): void

this.myService.get(1);
this.myService.get(2);



handleChanges(changes: any): void

console.log(changes);









And this is the same situation but solved in the second way:



my-service.service.ts



@Injectable()
export class MyService

constructor(private http: Http)

get(id: string): Observable<any>
return this.http
.get(`/some/url/$id`)
.map(response => response.json());





my-component.component.ts



@Component(...)
export class MyComponent implements OnInit

constructor(private myService: MyService)

ngOnInit(): void

this.myService.get(1).subscribe(
next =>
console.log(next);

);

this.myService.get(2).subscribe(
next =>
console.log(next);

);







So as you can see both examples are doing the same thing but in other ways. I have omitted unnecessary imports and other things because they are not connected with my problem.



I would say that the first solution is much cleaner since it gives me a possibility to have multiple calls to the same get() method and have one method which handles all of them.



On the other hand, if I had more different methods in my service and every one of them would have to use different Observable then it would make my service more complicated and low-readable.



Please advice which solution is better?









share|improve this question












share|improve this question




share|improve this question








edited Apr 7 at 17:47









jonrsharpe

12.9k12554




12.9k12554









asked Feb 27 at 10:06









Sebastian Kaczmarek

1063




1063




closed as off-topic by jonrsharpe, Sam Onela, Mast, t3chb0t, Stephen Rauch Apr 7 at 21:54


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Mast, t3chb0t, Stephen Rauch
If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by jonrsharpe, Sam Onela, Mast, t3chb0t, Stephen Rauch Apr 7 at 21:54


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Mast, t3chb0t, Stephen Rauch
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 1




    As you note, there are different contexts, so there is no "better" - the crucial benefit of the first version is that other components (or services) could also be receiving the updated data, whereas in the second version they'd have to make their own calls (and trigger their own requests). See e.g. my answer stackoverflow.com/a/41554338/3001761 for a neater way to implement it.
    – jonrsharpe
    Apr 7 at 17:52






  • 1




    "Let me show a minimal example." Nonononono, we don't do MCVE, that's Stack Overflow. Please share your actual code and take a look at the help center.
    – Mast
    Apr 7 at 19:10












  • 1




    As you note, there are different contexts, so there is no "better" - the crucial benefit of the first version is that other components (or services) could also be receiving the updated data, whereas in the second version they'd have to make their own calls (and trigger their own requests). See e.g. my answer stackoverflow.com/a/41554338/3001761 for a neater way to implement it.
    – jonrsharpe
    Apr 7 at 17:52






  • 1




    "Let me show a minimal example." Nonononono, we don't do MCVE, that's Stack Overflow. Please share your actual code and take a look at the help center.
    – Mast
    Apr 7 at 19:10







1




1




As you note, there are different contexts, so there is no "better" - the crucial benefit of the first version is that other components (or services) could also be receiving the updated data, whereas in the second version they'd have to make their own calls (and trigger their own requests). See e.g. my answer stackoverflow.com/a/41554338/3001761 for a neater way to implement it.
– jonrsharpe
Apr 7 at 17:52




As you note, there are different contexts, so there is no "better" - the crucial benefit of the first version is that other components (or services) could also be receiving the updated data, whereas in the second version they'd have to make their own calls (and trigger their own requests). See e.g. my answer stackoverflow.com/a/41554338/3001761 for a neater way to implement it.
– jonrsharpe
Apr 7 at 17:52




1




1




"Let me show a minimal example." Nonononono, we don't do MCVE, that's Stack Overflow. Please share your actual code and take a look at the help center.
– Mast
Apr 7 at 19:10




"Let me show a minimal example." Nonononono, we don't do MCVE, that's Stack Overflow. Please share your actual code and take a look at the help center.
– Mast
Apr 7 at 19:10















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation