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

HTML data in Vanilla Javascript with Ajax #30

Open
mathieupreaud opened this issue Nov 8, 2023 · 0 comments
Open

HTML data in Vanilla Javascript with Ajax #30

mathieupreaud opened this issue Nov 8, 2023 · 0 comments

Comments

@mathieupreaud
Copy link

mathieupreaud commented Nov 8, 2023

I have an Ajax function written in Javascript to load the items while scrolling with Colcade.

The issue is that I can't find the way to convert this jQuery line of code to Javascript. I would like to wrap the Ajax data into an objet and use it to parse the HTML.

const posts = $(data);

I'm using colc.append(posts) to load the items. However I'm not sure if posts has to be a jQuery object?

The JS:

const grid = document.querySelector(".js-grid");

    if (grid) {
        
        // Colcade JS
        var colc = new Colcade(grid, {
            columns: '.js-grid-col',
            items: '.js-grid-article'
        });
        
        // Ajax
        const current_post_type = grid.dataset.currentPostType;
        const posts_myajax = grid.dataset.postsMyajax;
        let current_page_myajax = grid.dataset.currentPageMyajax;
        const max_page_myajax = grid.dataset.maxPageMyajax;
        const ajax_url = grid.dataset.ajaxUrl;
        const current_action = grid.dataset.action;
    
        var throttleTimer;
        const throttle = (callback, time) => {
            if (throttleTimer) return;
    
            throttleTimer = true;
    
            setTimeout(() => {
                callback();
                throttleTimer = false;
            }, time);
        };
    
        const handleInfiniteScroll = () => {
            throttle(() => {
                const endOfPage = window.innerHeight + window.pageYOffset >= document.body.offsetHeight;
            
                if (endOfPage) {
                
                    const data = new FormData();
    
                    data.append( 'action', current_action );
                    data.append( 'archive_post_type', current_post_type );
                    data.append( 'query', posts_myajax );
                    data.append( 'page', current_page_myajax );
    
                    fetch(ajax_url, {
                    method: "POST",
                    body: data
                    })
                    .then((response) => response.text())
                    .then((data) => {
                        if (data) {

                            const posts = $(data);
                            colc.append(posts);

                            current_page_myajax++;
                        }
                    });
    
                }
            
                if (current_page_myajax == max_page_myajax) {
                    removeInfiniteScroll();
                }
    
            }, 1000);
        };
    
        const removeInfiniteScroll = () => {
            window.removeEventListener("scroll", handleInfiniteScroll);
        };
    
        window.addEventListener("scroll", handleInfiniteScroll);
    
    }

Thank you.

EDIT

Following to a discussion on a forum, the equivalent in Vanilla javascript would be using DOMParser() to parse the HTML:

const parser = new DOMParser();
const posts = parser.parseFromString(data, "text/html");
colc.append(posts);

However this produces the following error in the console:

Unhandled Promise Rejection: HierarchyRequestError: The operation would yield an incorrect node tree

The error points the Colcade JS file fragment.appendChild( elem );

proto.getQueryItems = function( elems ) {
  elems = makeArray( elems );
  var fragment = document.createDocumentFragment();
  elems.forEach( function( elem ) {
    fragment.appendChild( elem );
  });
  return querySelect( this.options.items, fragment );
};
@mathieupreaud mathieupreaud changed the title HTML data in javascript with Ajax HTML data in Vanilla Javascript with Ajax Nov 8, 2023
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

1 participant