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

Javascript in cshtml not needed #2

Open
mrdnote opened this issue Apr 5, 2020 · 1 comment
Open

Javascript in cshtml not needed #2

mrdnote opened this issue Apr 5, 2020 · 1 comment

Comments

@mrdnote
Copy link

mrdnote commented Apr 5, 2020

Hi, thanks for this example, works nicely!

One remark, there is not really a need for the javascript in _Host.cshtml if you change the constructor of InfiniteScroll to be parameterless and add an Init method to pass the element id and DotNetHelper:

this.Init = function (elementId, dotNetHelper) {
    this.element = document.getElementById(elementId);
    this.dotNetHelper = dotNetHelper;
}

and then instantiate the class in the bottom for the .js file:

window.ScrollList = new InfiniteScroll();

From there you can remove the code from Index.cshtml:

/* commented redunant code:
window.ScrollList = {
    Init: function (ele, ref) {
        window.ScrollList.ifs = new InfiniteScroll(ele, ref);
    },
    RemoveListener: function () {
        window.ScrollList.ifs.RemoveListener();
    }             
}
*/
@YaarPatandarAA
Copy link

YaarPatandarAA commented Oct 5, 2020

class InfiniteScroll {
    constructor() {
        this.Init = function (elementId, dotNetHelper) {
            this.element = document.getElementById(elementId);
            this.DotNetHelper = dotNetHelper;
        }
        
        this.ListenToScroll = function (event,element,DotNetHelper) {
            var bounding = element.getBoundingClientRect();
            if (
                bounding.top >= 0 &&
                bounding.left >= 0 &&
                bounding.right <= (window.innerWidth || element.clientWidth) &&
                bounding.bottom <= (window.innerHeight || element.clientHeight)
            ) {
                console.log('In the viewport!');
                DotNetHelper.invokeMethodAsync("LoadMore");
            }
        };
        this.handler = (ev) => { this.ListenToScroll(ev, this.element, this.DotNetHelper) };

        document.addEventListener("scroll", this.handler, true);

        this.RemoveListener = function () {
            document.removeEventListener("scroll", this.handler, true);
            console.log('Listener removed!');
        }
    }
}

window.ScrollList = new InfiniteScroll();

Full InfiniteScroll.js, with the changes, if anyone wanted. Remember to remove code from _Host.cshtml.

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