Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Listener Not Working #121

Open
psmod2 opened this issue Nov 19, 2015 · 5 comments
Open

Change Listener Not Working #121

psmod2 opened this issue Nov 19, 2015 · 5 comments

Comments

@psmod2
Copy link

psmod2 commented Nov 19, 2015

Hello,

I'm find at times the following change listener does not work.

hoodie.store.on('change', refresh);

function refresh() {
  window.location.reload();
}

As this been reported/observed already?

Thanks.

@gr2m
Copy link
Member

gr2m commented Nov 19, 2015

Not that I would be aware of. Can you provide a bit more context? Does the change event not get triggered when you edit data locally, or when changes come in from remote?

Also why would you reload the page on a change event in store?

@psmod2
Copy link
Author

psmod2 commented Nov 19, 2015

I'm using:

hoodie.store.on('change', refresh);

With the understanding that if my couchDB changed the refresh method would be called. Is the right way?

And for context, the app i'm implementing has 2 sides, a customer facing and back end, when a change is made on either i.e. someone logs in or out there should a refresh to ensure the new data is displayed.

@gr2m
Copy link
Member

gr2m commented Nov 19, 2015

Ideally you would update the HTML dynamically if changes occur, instead of reloading the page.

I could imagine that the window.location.reload() could cause trouble, as JavaScript might stop doing everything it does, and instead reload the page. So maybe some operations get aborted, like persisting a change from remote in localStorage.

If you need to do the reload, can you try putting in a delay, like this?

hoodie.store.on('change', refresh);

var timeout
function refresh() {
  clearTimeout(timeout)
  timeout = setTimeout(location.reload.bind(location), 300)
}

That code will reload the page 300ms after the last change event

@psmod2
Copy link
Author

psmod2 commented Nov 19, 2015

That helps.

In addition I think i found the underlying issue, there was a location.reload() being executed which i believe was stopping the JS from doing what it does as you state.

Will continue testing.

@gr2m
Copy link
Member

gr2m commented Nov 19, 2015

Thanks, glad to hear you make progress. Feel free to close this issue and open new ones as they arise

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

No branches or pull requests

2 participants