0

I am working on a social feed that displays charitable activities. It's supposed to look like the second activity in this image:

enter image description here

But as you can see, every time that angular-infinite-scroll goes to fetch the next batch of activities, it adds a broken template to the top of the feed.

This is the function that's called to get more activities:

feedService.prototype.nextPage = function () {
    var fs = this;

    if (fs.busy) return;
    fs.busy = true;

    fs.params.limit = fs.params.limit || _feedLimit;

    if (fs.activities.length > 0)
      fs.params.idLessThan = fs.activities[fs.activities.length - 1].id;

    $http({ url: serviceBase + 'api/stream/getflatfeed', method: 'GET', params: fs.params })
    .then(function (response) {
      var feed = response.data;
      console.dir(feed.results);
      console.dir(fs.activities);
      if (feed) {
        fs.duration = feed.duration;
        fs.gStreamMessage = feed.gStreamMessage;
        fs.unread = feed.unread;
        fs.unseen = feed.unseen;
        fs.noMoreActivity = false;
        if (response.data.results && response.data.results.length === 0)
          fs.noMoreActivity = true;
        else {
          var acts = _decorateFeedActivities(response.data.results, fs.params);
          acts.forEach(function (act) {
            var alreadyExists = false;
            fs.activities.forEach(function (e, i) {
              if (e.id === act.id) {
                console.log('alreadyExists!');
                alreadyExists = true;
              }
            });
            if (!alreadyExists) {
              fs.activities.push(act);
            }
          });
        };
      }
      fs.busy = false;
    }.bind(this));
  };

This is the directive used for activities:

feedModule.directive('feedCard', ['$compile', '$http', '$filter', function ($compile, $http, $filter) {
  var getTemplate = function (contentType) {
    var templateLoader,
    baseUrl = 'app/areas/feed/cards/',
    templateMap = {
      donated: 'general-donation-card-tpl.html',
      image: 'image-card-tpl.html',
      video: 'video-card-tpl.html',
      quote: 'quote-card-tpl.html',
      link: 'link.html',
      chat: 'chat.html',
      audio: 'audio.html',
      answer: 'answer.html'
    };
    var templateUrl = baseUrl + templateMap[contentType];
    templateLoader = $http.get(templateUrl);
    return templateLoader;
  };

  return {
    restrict: 'E',
    scope: {
      activity: '=',
      user: '='
    },
    replace: true,
    link: linker
  };

  function linker(scope, element) {
    var loader = getTemplate(scope.activity.verb);
    var promise = loader.success(function (html) {
      element.html(html);
    }).then(function (response) {
      element.replaceWith($compile(element.html())(scope));
      console.log('---------------');
    });
  }
}]);

And this is the HTML for the infinite-scroll directive and the activities within:

<div class="infinite-scroll" infinite-scroll='feed.nextPage()' infinite-scroll-disabled='feed.busy || feed.noMoreActivity'>
        <feed-card class="portlet light bordered feed-card message-card" ng-repeat="activity in feed.activities" activity="activity" user="data.user"></feed-card>
        <div class="well" ng-show='feed.busy'>Loading data...</div>
        <div class="well" ng-show='feed.noMoreActivity'>There is no more activity on your feed...</div>
      </div>

I'm at a loss for what's happening. The additional, broken activities don't appear in any arrays of API returns. I would assume that it's maybe some sort of scoping issue that would cause the directive to fire twice, with one of them being broken?

If anyone has experienced anything like this, I would love to hear your advice. Thank you.

ClaytonAlanKinder
  • 193
  • 1
  • 3
  • 13

1 Answers1

0

Fixed this by adding "ng-if='activity'" to the feed-card element.

ClaytonAlanKinder
  • 193
  • 1
  • 3
  • 13