Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Observer set on routeData keeps firing in the background after navigating to a different page/element. #201

Open
vedtam opened this issue Jun 27, 2017 · 3 comments

Comments

@vedtam
Copy link

vedtam commented Jun 27, 2017

Hi,

Is it a normal behaviour for an observer set on routeData to fire after navigating away from an element?
I use the stock PSK, with iron-pages. I have created two pages: A, B - each having app-location and an observer set for routeData so when this changes, data gets fetched from the server.

The issue is, page A starts making requests when I navigate to page B.
Should I somehow unregister the observers manually?

Expected outcome

Observer set in page A on routeData should be removed once the user navigates to page B.

page A:

<app-location route="{{route}}"></app-location>
<app-route
    route="{{route}}"
    pattern="/:page"
    data="{{routeData}}"
    tail="{{subroute}}">
</app-route>
<app-route
    route="{{subroute}}"
    pattern="/:id"
    data="{{tail}}">
</app-route>


...

static get observers() {
  return [
    'pageChanged(routeData.page)',
  ];
}


pageChanged(page){
  console.log('page A fired. ' + this.tail.id);
  this._fetchProduct();
}

page B:

<app-location route="{{route}}"></app-location>
<app-route
    route="{{route}}"
    pattern="/:page"
    data="{{routeData}}"
    tail="{{subroute}}">
</app-route>
<app-route
    route="{{subroute}}"
    pattern="/:id"
    data="{{tail}}">
</app-route>

...

static get observers() {
  return [
    'pageChanged(routeData.page)',
  ];
}


pageChanged(page){
  console.log('page B fired. ' + this.tail.id);
  this._fetchProfile();
}
@CaptainCodeman
Copy link

Yes, once you load a view it stays loaded so if you've set an observer it will keep updating. I tried to explain what's going on and give some options to make the non-active views inert in this blog post. There are a few different approaches you can use.

@vedtam
Copy link
Author

vedtam commented Jul 16, 2017

@CaptainCodeman thanks for the info, I was using a solution like nr. 2 in your blog post, but nr. 1 seems attractive as well.

Thanks!

@dannywolfmx
Copy link

You can also use dom-if and visible (iron-pages) to activate or deactivate the app-route. In this way the app-route of the children pages will not do anything if they are not being visible.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants