0

I have 2 divisions, for example divisions Left and Right, the server side urls for these divisions are the same except for the division name for example:

'/api/Left/DoThisThing';

or

'/api/Right/DoThisThing';

I have this in my HttpService (simplified):

@Injectable()
export class MyHttpService {

    constructor(private http: Http) { }

    private baseUrl = '/api/';
    private selectedDivision: Divisions = Divisions.Left;

    getAll(): Observable<Test[]> {
        return this.http.get(this.baseUrl + Divisions[this.selectedDivision])
            .map((response: Response) => response.json())
            .catch((error: any) => Observable.throw(error.json().error || 'An unknown server error occurred while trying to get all.'));
    }

    selectDivision(division: Divisions) { this.selectedDivision = division; }
}

Of course, the problem is that calling selectDivision(Divisions.Right) does nothing until I click on the nav link that forces the child component that subscribes to getAll() to reload.

The way I'd prefer to do this is to notify all subscribers of getAll() to retry that endpoint... or perhaps cause that get request to retry with the newly updated url. How do I do it? If there a better way to do this?

Serj Sagan
  • 28,927
  • 17
  • 154
  • 183
  • Have you tried to use the [**retry**](http://reactivex.io/documentation/operators/retry.html) *operator*? – developer033 Jun 16 '17 at 00:07
  • thanks for your comment, and no I have not, can you please give an example in the context of the question? Where/how would I call this? I just tried adding `this.getAll().retry();` to the `selectDivision()` method and that didn't do anything – Serj Sagan Jun 16 '17 at 00:14
  • You can pass the number of attempts that you want to the `retry`, ex: `retry(3)`.. otherwise it will be executed indefinitely. Check [**this**](https://angular-2-training-book.rangle.io/handout/http/catching-rejections/retry.html). – developer033 Jun 16 '17 at 00:16
  • no, this didn't work either, this seems like a deal for the initial call, I need more like a `forceNewRetry()` or something like that – Serj Sagan Jun 16 '17 at 00:23
  • I ended up using a hack to get around this problem: https://stackoverflow.com/a/44580036/550975 but would love to see a valid response to this – Serj Sagan Jun 16 '17 at 02:56

0 Answers0