简体   繁体   English

带有离子框架无限滚动的instagram feed不会阻止重复器错误中的重复项

[英]instagram feed with ionic framework infinite-scroll doesn't stop duplicates in repeater error

I'm trying to build an IOS and android app with Ionic Framework that has an Instagram Feed with the instagram api that shows all photos shared by hashtag (I'll use the hashtag chocolat has an example) and I wanted to implement pull to refresh and infinite scrolling, so basicaly I would have a button that once clicked opened a view with the instagram feed and pulled the first 10 results, pull to refresh would try to get newer results, then if the feed was scrolled down it would add the next set of results (older results). 我正在尝试使用Ionic Framework构建IOS和android应用程序,该应用程序具有带有instagram api的Instagram Feed,该feed显示所有由hashtag共享的照片(我将使用hashtag chocolat给出一个示例),并且我想实现pull刷新和无限滚动,所以基本上我会有一个按钮,一旦单击该按钮,就会打开instagram feed的视图并拉出前10个结果,拉动刷新将尝试获得更新的结果,然后,如果feed向下滚动,它将添加下一个结果集(旧结果)。 I've been searching and I've built something that isn't working quite as intended. 我一直在搜索,并且已经构建了无法正常工作的产品。 I'm a newbie developer in ionic, angularjs and javascript so i decided to post my question here. 我是ionic,angularjs和javascript的新手开发人员,所以我决定在这里发布我的问题。

So here is my code: 所以这是我的代码:

The factory or service to get the instagram json by http: 通过http获取instagram json的工厂或服务:

app.factory('PhotoService', function($http){ 
  var BASE_URL = "https://api.instagram.com/v1/tags/chocolat/media/recent?access_token=+++"; 
  //access_token hidden for privacy reasons
  var items = [];
  var nextUrl = 0;

  return {
    GetFeed: function(){
      return $http.get(BASE_URL+'&count=10').then(function(response){
        items = response.data.data;
        nextUrl = response.data.pagination.next_url;

        return items;

        });
    },
    GetNewPhotos: function(){
      return $http.get(BASE_URL+'&count=2').then(function(response){
        items = response.data.data;

        return items;
      });
    },
    GetOldPhotos: function(){
      return $http.get(nextUrl).then(function(response){

        items = response.data.data;
        return items;
      });
    }
  }
});

The controller: 控制器:

app.controller('CivrInstagramController', function($scope, $timeout, PhotoService) {
  $scope.items = [];
  $scope.newItems = [];

  PhotoService.GetFeed().then(function(items){
    $scope.items = items;
  });

  $scope.doRefresh = function() {
    if($scope.newItems.length > 0){
      $scope.items = $scope.newItems.concat($scope.items);

     //Stop the ion-refresher from spinning
     $scope.$broadcast('scroll.refreshComplete');

     $scope.newItems = [];
    } else {
      PhotoService.GetNewPhotos().then(function(items){
        $scope.items = items.concat($scope.items);

        //Stop the ion-refresher from spinning
        $scope.$broadcast('scroll.refreshComplete');
      });
    }
  };

  $scope.loadMore = function(){
    PhotoService.GetOldPhotos().then(function(items) {
      $scope.items = $scope.items.concat(items);

      $scope.$broadcast('scroll.infiniteScrollComplete');
    });
  };
});

The view: 风景:

<ion-view view-title="#Chocolat Photos!">
  <ion-content>
    <ion-refresher on-refresh="doRefresh()"></ion-refresher>
    <div class="list card" ng-repeat="item in items track by item.id">
      <div class="item item-avatar">
        <img ng-src="{{item.user.profile_picture}}" ng-  if="item.user.profile_picture.startsWith('https')">
        <h2>{{item.user.full_name}}</h2>
        <p><span am-time-ago="item.created_time" am-preprocess="unix"></span></p>
      </div>

      <div class="item item-body" >
        <img ng-src="{{item.images.low_resolution.url}}" ng-if="item.images.low_resolution.url.startsWith('https')">
        <p>
          {{item.caption.text}}
        </p>
      </div>

    </div>
    <ion-infinite-scroll on-infinite="loadMore()" distance="5%"></ion-infinite-  scroll>
  </ion-content>
</ion-view>

The PhotoService.GetFeed() is working well as it populates correctly the first 10 photos, however, when I try to infinite-scroll I get the error: PhotoService.GetFeed()运行良好,因为它正确填充了前10张照片,但是,当我尝试无限滚动时,出现错误:

Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys.

I'm using track by item.id as you can see in the view, so i think this means im getting the same results over and over again with the PhotoService.GetOldPhotos() . 我正在使用在项目视图中看到的by item.id跟踪,所以我认为这意味着Im通过PhotoService.GetOldPhotos()一遍又一遍地获得相同的结果。 as for the PhotoService.GetNewPhotos() it works when there is new data to display but it throws the same error of duplicates in a repeater when there is no new data to fetch (no new posts with #chocolat) 至于PhotoService.GetNewPhotos(),它在有新数据要显示时起作用,但是在没有新数据要提取时(在#chocolat中没有新帖子),它会在转发器中引发相同的重复错误。

What am i doing wrong? 我究竟做错了什么? can anyone give me some advice? 谁能给我一些建议? Thanks in advance for your time! 在此先感谢您的时间! Here is a plnkr ++ link to my project where you can actually see it trying to work :\\ 这是指向我的项目的plnkr ++链接,您可以在其中实际看到它正在工作:\\

++ you might need to enable cross-origin resource sharing in your browser to see the feed working. ++,您可能需要在浏览器中启用跨域资源共享才能看到提要。

EDIT1 : ALMOST DONE! 编辑1:几乎完成了!

So after some long hours and after some console.log()'s :p I was able to get a instagram feed by tag and make it work with ionic pull to refresh and ionic infinite scrolling (well 90% I guess) but I'm still with a problem with my code, because every time I scroll to the last set of items I can't stop infinite scroll nor loadMore() and they will try to load more data, because the last nextUrl is undefined but instagram-api accepts the &next_max_tag_id=undefined and gets me the most recent results again and this results in an error because there can be no duplicates in ng-repeat and I dont want to show the first results again. 因此,经过很长时间之后,经过了console.log()的:p之后,我能够通过标签获取instagram feed,并使其与离子拉动一起刷新和离子无限滚动(我猜是90%),但是我我的代码仍然存在问题,因为每次我滚动到最后一组项目时,我都无法停止无限滚动或loadMore(),它们将尝试加载更多数据,因为最后一个nextUrl是未定义的,但是instagram-api接受&next_max_tag_id = undefined并再次获取我最近的结果,这将导致错误,因为ng-repeat中没有重复项,并且我不想再次显示第一个结果。

I just need to stop loadMore and infinite-scroll when there are no more results, or if &next_max_tag_id=undefined, or if there are duplicates in both arrays (but I don't think this is recommended performance wise). 当没有更多结果时,或者&next_max_tag_id = undefined,或者两个数组中都有重复项时,我只需要停止loadMore和infinite滚动即可(但是我不建议您在性能方面考虑)。 So here is a plnkr with my project, thank you for your time! 所以这是我的项目的项目,谢谢您的宝贵时间!

I changed the tag to something with low results so you can actually see the error without getting tired of scrolling :p 我将标签更改为结果较差的内容,因此您实际上可以看到错误,而不必厌倦滚动:p

We solved this separately, but I figured I'd post the answer here too. 我们单独解决了这个问题,但我想我也将答案发布在这里。 The main changes are 主要变化是

  1. Switch to JSONp to avoid CORS restraints 切换到JSONp以避免CORS限制
  2. Use instagram's min_tag_id and max_tag_id format 使用instagram的min_tag_idmax_tag_id格式
  3. Update the getOldPhotos() method to automatically return an empty array when there is no next_max_tag_id (Technically, it returns a different promise that's already resolved to an empty array.) 更新getOldPhotos()方法以在没有next_max_tag_id时自动返回一个空数组(从技术上讲,它返回一个已经解析为空数组的承诺)。
  4. Change $scope.loadMore() to check for an empty response and set a flag to kill the <ion-infinite-scroll> 更改$scope.loadMore()以检查是否为空,并设置一个标志来杀死<ion-infinite-scroll>

See a full working copy here: http://plnkr.co/edit/LBW0pp?p=preview 在此处查看完整的工作副本: http : //plnkr.co/edit/LBW0pp?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM