1

I have two screens:

  1. Screen1(Empleado-empleado-form.js, empleado-form.js is called from empleado.js) where Service1 is injected.
  2. Screen2(Departamento-departamento.js) where Service 2 is injected

This is a repro of my error:

  1. When navigating to Screen1 the Service1 is injected.
  2. When navigating to Screen2 Service2 is injected.
  3. When navigating back to Screen1, Service2 is injected.

I suppose that the intented behavior is that each Service is injected into the screen with @injected not to randomly inject to the next Screen.

This is the github's project: https://github.com/DiegoGallegos4/aurelia-test;

  • Screen 1 is located: src/departamento(services is inside)
  • Screen 2 is located: src/empleado(service is inside, empleado-form.js is where empleado-service.js is injected.)

I use the following pattern:

@inject(Screen1Service)
export class Screen1{
    constructor(service){
        this.service = service
    }

    ...service use to fetch/post data
}

If a repro is needed, let me know I can make a gist using promises. However, I dont know is navigation would be available on gist.

Diego Gallegos
  • 1,722
  • 2
  • 18
  • 31
  • How do you know "When navigating back to Screen1, Service2 is injected"? – qtuan May 04 '16 at 04:53
  • That's why I infer. On both screens, there is a custom-table component and when you navigate back to Screen 1. The data fetched from Service2 in seen in the custom-table component. – Diego Gallegos May 04 '16 at 05:08
  • I believe the injection was not wrong. Problem lies in the way you config `HttpClient`. To confirm it, can you `console.log(this.service)` in screen1's `activate()`? – qtuan May 04 '16 at 05:14
  • The service is injected correctly. What changes on navigation is the url. Somehow the url of Service2 is used on Service 1 when navigating back. Why is so? – Diego Gallegos May 04 '16 at 05:29

1 Answers1

0

The service injection was indeed correct. The problem lies in the way these services use HttpClient.

Each service class configure HttpClient in its constructor. Since a single HttpClient in injected into all services, the latter created service override previous configuration. Moreover, each service is a singleton itself, so its constructor and thus HttpClient configuration only run once.

Therefore, the navigation order matters. When navigate from Screen1 -> Screen2 -> back to Screen1 , the last configuration in Service2 has effect. If you change the sequence to Screen2 -> Screen1 -> Screen2, you will observe the opposite.

To fix this issue, you should configure HttpClient only at one place, e.g in main or app. See this discussion

Community
  • 1
  • 1
qtuan
  • 687
  • 4
  • 10
  • How would you configure the api url for each specific url or you would put in each fetch? – Diego Gallegos May 04 '16 at 21:31
  • I would configure the API base URL (well, the config method name is `withBaseUrl` for a purpose), and use relative URL in each fetch. E.g in your case, base URL can be `http://hrm.dev/api` – qtuan May 05 '16 at 02:46
  • or use aurelia-api. it lets you define endpoints in a nicely fashion: http://aurelia-api.spoonx.org/configuration.html – fops May 06 '16 at 07:34