0

I am using infinite scroll to load posts and tried to integrate a custom like button for every single posts that needs a small jquery script to work. My problem is I added this Jquery directly after the sucess in ajax load posts. But when I load eg the 3. page my jquery script executes twice and on the posts of the 2nd page the lke buttons are not working correctly. How can I handle this? If I dont execute the code after the ajax request and only call this jquery code globally the like buttons do not work in the new loaded posts of the ajax infinite scroll. Maybe I need to stop the sript of before when eg loadin 3. page through the ajax infinite scroll but how? This is my code:

function load_more_posts(selector){
    var url = $(selector).attr('href');
    var data;
    loading = true;

    $.ajax({
        url: url,
        data: data,
        success: function( data ) {

            var $items = $( '.loading-content .item', data );
                $new_anchor = $( selector, data );
                $items.addClass('hidden');

            if ( $('#cooked-plugin-page .result-section.masonry-layout .loading-content').length ){
                $( '.loading-content').isotope( 'insert', $items );

            } else {
                $( '.loading-content').append($items);
                setTimeout(function() {
                    $items.removeClass('hidden');
                }, 200);
            }

            if($new_anchor.length) {
                $(selector).attr('href', $new_anchor.attr('href'));
            } else {
                $(selector).remove();
            }

    loading = false;

            $('.like-btn').each(function() {
        var $button      = $(this),
            $icon        = $button.find('> i'),
            likedRecipes = $.cookie('cpLikedRecipes'),
            recipeID     = $button.attr('data-recipe-id');
            cookied      = $button.attr('data-cookied');
            userLiked    = $button.attr('data-userliked');

        if ( cookied == 1 && typeof likedRecipes !== 'undefined' && likedRecipes.split(',').indexOf(recipeID) > -1 || userLiked == 1 ) {
            $icon.removeClass('fa-heart-o').addClass('fa-heart');
        }

    });

    $('#cooked-plugin-page .like-btn').on('click', function() {
        var $button      = $(this),
            $icon        = $button.find('> i'),
            $count       = $button.find('.like-count'),
            count        = parseInt($count.text()),
            likedRecipes = $.cookie('cpLikedRecipes'),
            recipeID     = $button.attr('data-recipe-id'),
            cookied      = $button.attr('data-cookied'),
            likeURL      = $button.attr('href'),
            likeAction;

        if ( $icon.hasClass('fa-heart-o') ) {
            $icon.removeClass('fa-heart-o').addClass('fa-heart');
            count++;

            if (cookied == 1){
                if ( typeof likedRecipes === 'undefined' ) {
                    likedRecipes = recipeID;
                } else {
                    likedRecipes = likedRecipes + ',' + recipeID;
                }

                $.cookie('cpLikedRecipes', likedRecipes, { expires: 365 } );
            }

            likeAction = 'like';
        } else {
            $icon.removeClass('fa-heart').addClass('fa-heart-o');
            count--;

            if (cookied == 1){
                if ( typeof likedRecipes === 'undefied' ) {
                    return false;
                }
            }

            if (cookied == 1){
                var likedSplit = likedRecipes.split(','),
                    recipeIdx  = likedSplit.indexOf(recipeID);

                if ( recipeIdx > -1 ) {
                    likedSplit.splice( recipeIdx, 1 );
                    likedRecipes = likedSplit.join(',');

                    $.cookie('cpLikedRecipes', likedRecipes, { expires: 365 } );

                    likeAction = 'dislike';
                }
            } else {
                likeAction = 'dislike';
            }

        }

        $.ajax({
            'url' : likeURL,
            'data': {
                'action'    : 'cp_like',
                'likeAction': likeAction
            },
            success: function(data) {
                $count.text(data);
            }
        });

        return false;
    });

    $('#cooked-plugin-page .tab-links a').on('click', function() {
        var tab = $(this).attr('href');

        if ( !$(this).is('.current') ){
            $(this).addClass('current').siblings('.current').removeClass('current');
            $('#cooked-plugin-page.fullscreen .tab').removeClass('current');
            $(tab).addClass('current');
            $win.scrollTop(0);
        }

        return false;
    });

    if($('.rating-holder').length) {
        $('.rating-holder .rate')
            .on('mouseenter', function() {
                var $me = $(this);
                var $parent = $me.parents('.rating-holder');
                var my_index = $me.index();
                var rated = $parent.attr('data-rated');
                $parent.removeClass(function(index, css) {
                    return (css.match (/(^|\s)rate-\S+/g) || []).join(' ');
                });
                $parent.addClass('rate-' + (my_index + 1));
            })
            .on('mouseleave', function() {
                var $me = $(this);
                var $parent = $me.parents('.rating-holder');
                var my_index = $me.index();
                var rated = $parent.attr('data-rated');
                $parent.removeClass(function(index, css) {
                    return (css.match (/(^|\s)rate-\S+/g) || []).join(' ');
                });
                if(rated !== undefined) {
                    $parent.addClass('rate-' + rated);
                }
            })
            .on('click', function() {
                var $me = $(this);
                var $parent = $me.parents('.rating-holder');
                var my_index = $me.index();
                $('.rating-real-value').val(my_index + 1);
                $parent.attr('data-rated', my_index + 1);
                $parent.addClass('rate-' + (my_index + 1));
            });
    }   

            setTimeout(function() {
                masonry();
            }, 500);

        }
    });

}
user45836
  • 13
  • 7

1 Answers1

0

There's a great plugin for scrolling. He has methods such as bund, unbind, destroy and e.t.c.:

https://github.com/infinite-scroll/infinite-scroll#methods
ilgam
  • 3,186
  • 1
  • 28
  • 25