0

Hi I have the following recommendation from an expert and I am trying to rebuild my Code based on these recommendations:

  • from $.each() you can return true or false. If you return false, the loop stops.
  • Try not to build HTML from concatenated strings. This is prone to XSS vulnerabilities that are easy to avoid. jQuery gives you the tools to build HTML safely.
  • Generally, for the same reason, try to avoid working with .html(), especially if you already have DOM elements to work with.
  • Don't use inline event handlers like onclick. At all. Ever.

This is the new Code I am working on:

var page = 1;
$(document).on('click', '#devotionclick', function blogs() {
    $('#postlist').empty();
   // $('#category').prepend('<div class="categories_listing"><span data-type="blogs" data-category="5">Blog Category</span></div>');
    var count = "5";
    var result = $.getJSON('http://howtodeployit.com/api/get_posts/?count=' + count + '&page=' + page, function (data, status) {
        if (data !== undefined && data.posts !== undefined) {
            $.each(data.posts, function (i, item) {    
                var str = item.title;

                $('#postlist').append('<div class="article"' + item.id + '"><div>' + item.title + '</div><div>' + item.excerpt + '</div></div>');
                if (data !== undefined) {
                    $('#stats').text('Page ' + data.query.page + ' of ' + data.pages + ' | Total posts ' + data.count_total + '');
                }
                if (data.query.page < data.pages) {
                    $("#loadmore").show();
                } else {
                    $("#loadmore").hide();
                }
            });
            page++;
        }
    });
    $('#postlist').append('<div id="loadmore"><div id="stats"></div><div id="loadmore">load more</div></div>');
    $('#loadmore').click(blogs);
});

HTML:

!-- Page: home -->
    <div id="home" data-role="page">
        <div class="ui_home_bg" data-role="content"></div>
        <div data-role="listview">
            <a href="#devotion" id="devotionclick" data-role="button">Daily Devotional Messages</a>
        </div><!-- links -->
    </div><!-- page -->

<!-- Page: Daily Devotional Messages -->
    <div id="devotion" data-role="page">
        <div data-role="header" data-position="fixed">
            <h2>Daily Devotional Messages</h2>
        </div><!-- header -->
        <div data-role="content" id="postlist"> </div><!-- content -->
    </div><!-- page -->

The issues I am having right now is:

  1. When I click on the Button it Loads the first 5 Posts but when I click on the 'load more' Text, it Loads the next 5 rather than Appending to existing Lists.

  2. The Lists isn't displayed as a Listview item which should be clickable

1 Answer 1

1

Problem 1
It is because of $('#postlist').empty(); in the click handler.... you are removing all items from the page before loading new items. Remove this

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.