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

"In view" state could change not only by scrolling #24

Open
slavafomin opened this issue Apr 26, 2018 · 3 comments
Open

"In view" state could change not only by scrolling #24

slavafomin opened this issue Apr 26, 2018 · 3 comments
Assignees

Comments

@slavafomin
Copy link
Contributor

Hello!

Thank you for this great library!

However, are you only using scrolling event to determine the point in time when "in view" state should be updated? It looks like this.

However, this is not true in some circumstances. On dynamic page, the element could get out of the user's view not only when user is scrolling away, but also, when another element on the page pushes the watched element out of the view.

I'm implementing an infinite scrolling feature in our web application, but I'm getting an incorrect behavior, because I'm relying on "in view" state change events to determine if more items should be loaded. The reason for this is that more items are loaded dynamically and added to the table, this in effect pushes the table footer out of the view, but the "in view" state is not updated accordingly, so the app continues to load more items, until user scrolls the page at least a bit.

What could you recommend to fix this behavior?

Is there a way to force the directive to update it's state programmatically?

Thanks!

@slavafomin
Copy link
Contributor Author

By looking at source code of the library I've found a workaround:

window.document.dispatchEvent(new Event('scroll'));
setTimeout(() => {
  this.loading$.next(false);
}, 0);

I have to manually trigger scroll event on window.document object in order for directive to update it's state. Then, I'm checking the updated state again in the next event loop.

However, this is cumbersome and not very convenient. I think the better workaround would be if library would provide some API to force it to update the state. Even better would be to find a way to update the state automatically. Maybe we could update it with every change detection?

Also, have you looked into native browser API for in-view detection? I saw an article on Smashing Magazine about it, maybe we could use it if it's supported by the browser and fallback to scroll/resize events.

@ajaysinghj8
Copy link
Owner

Hi @slavafomin ,

I got the point, and I am thinking about it, the best way possible. As of now can you try something the [trigger] input attribute, using trigger you can force recalculation of in view logic.

<div in-view-container (inview) =handler($event) [tigger]="tigger"> 
     // in view items
</div>
```js
  trigger: any = new BehaviorSubject(0);
 // on some condition 
 this.trigger.next(0)

@slavafomin
Copy link
Contributor Author

Okay, thanks. If you have some ideas how to solve this problem, I'm open to help.

Also, wouldn't it be easier to call the method directly on the directive? We could obtain a reference to it through @ViewChild(). It looks simpler than creating another BehaviorSubject.

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

No branches or pull requests

3 participants