使用Javascript数组限制Facebook Feed帖子的数量

时间:2016-12-01 10:24:29

标签: javascript arrays facebook facebook-graph-api facebook-javascript-sdk

我有一个Facebook Feed聚合器,显示我所有页面的最新帖子。我只希望一次显示10个帖子,然后“更多按钮下方显示更多帖子,如果点击。一切都是功能,除了它显示每个页面可以滚动的每个帖子。页面加载速度如此之快,因为成千上万帖子加载。我已经通过我的推特帖子实现了这一点,但由于某些原因没有Facebook。我已经发布了facebook和twitter代码,所以你可以比较。我需要帮助限制显示的帖子数量。谢谢

以下是该软件屏幕截图的链接。 https://gyazo.com/d1c2363d553de2e267c3eddea5857fc3

    this.morefacebookpagefeeds = function () {
    var obj = vm.currentfacebookpaging();
    var feedinfo = new Object();
    feedinfo.facebookfeeds = new Array();
    feedinfo.PagingList = new Array();
    var item = 0;
    obj.forEach(function (e, index, array) {
        item++;
        var parameters = e.next.split("?")[1];
        var page = ko.utils.arrayFirst(vm.currentfeedModelArray(), function (i) {
            return i.Id == e.PageId;
        });
        FB.api("/" + facebookuserid + "/feed?" + parameters, 'get', { access_token: facebookaccesstoken }, function (response) {
            response.data.forEach(function (feed, index, array) {
                var feedsdata = new Object();
                feedsdata.Description = feed.description;
                feedsdata.Picture = feed.picture;
                feedsdata.Name = feed.name;
                feedsdata.Link = feed.link;
                feedsdata.Message = feed.message;
                feedsdata.PageName = page.ProfileName != undefined ? page.ProfileName : "";
                feedsdata.PostDate = moment(new Date(feed.created_time)).format('ddd MMM DD YYYY hh:MM a');
                feedsdata.ProfileImage = page.ProfileImageUrl;
                feedsdata.PageUrl = page.ProfileUrl != undefined ? page.ProfileUrl : "";
                feedsdata.Id = page.ProfileId != undefined ? page.ProfileId : "";
                feedinfo.facebookfeeds.push(feedsdata);
                if (response.paging != null) {
                    feedsdata.Paging = new Object();
                    feedsdata.Paging.previous = response.paging.previous;
                    feedsdata.Paging.next = response.paging.next;
                    feedsdata.Paging.PageId = pageId;
                }
                feedinfo.PagingList.push(feedsdata.Paging);
            });

            if (item == array.length) {
                feedinfo.facebookfeeds.forEach(function (item) {
                    vm.currentfeedModelArray.push(item);
                });
                feedinfo.PagingList.forEach(function (item) {
                    vm.currentfacebookpaging.push(item);
                })
            }
        });
    });
}

微博

 this.twitterfeeds = function (sinceid) {
    if (sinceid) {
        var sinceidvalue = vm.currenttwitterfeedModelArray()[vm.currenttwitterfeedModelArray().length - 1].Id;
    }
    ajaxExt({
        type: "POST",
        throbberPosition: { my: "center center", at: "center center", of: $(window), offset: "5 0" },
        data: { sinceid: sinceidvalue },
        url: baseUrl + 'Social/TwitterFeed',
        showThrobber: true,
        success: function (data) {
            vm.feedModelArray.removeAll();
            if (sinceidvalue == undefined || sinceidvalue == null) {
                vm.currenttwitterfeedModelArray.removeAll();
            }
            if (data.Object == null || data.Object.length == 0) {
                $('#loadmoretwitter_btn').css('display', 'none');
            }
            data.Object.forEach(function (item) {
                vm.currenttwitterfeedModelArray.push(item);
            });


        }
    });
}

1 个答案:

答案 0 :(得分:0)

有一个限制参数:

FB.api('/' + pageId + '/feed', {access_token: facebookaccesstoken, limit: 10}, ...

关于分页:https://developers.facebook.com/docs/graph-api/using-graph-api/#paging

编辑:由于您要求获取有限数量的页面的示例,这里有一些(未经测试,快速和脏):

var currentIndex = 0;
function getNextItems() {
    for (var i = currentIndex, count = currentIndex + 10; i < count; i++) {
        if (obj[i]) {
            FB.api(...);
        } else {
            return;
        }
        currentIndex++;
    }
}

...点击“更多”,再次调用该功能。