2016-04-13 7 views
0

私は私のコントローラで約束を使用しています。しかし時々それはちょうど永久にロードされ、WordPress.getAllCategories()関数は呼び出されません。AngularJS/Ionic約束 - ときどきページが永久にロードされる

これは私のコントローラです: var mod = angular.module( 'app.controllers.home'、[]);

mod.controller('HomeCtrl', function ($scope, $q, $sce, $ionicPlatform, WordPress, Loading) { 
    console.log('HomeCtrl init'); 

    $scope.$on('$ionicView.enter', function() { 
    Loading.show(); 

    WordPress.getAllCategories() 
     .then(function (cats) { 
     console.info(angular.toJson(cats)); 
     console.info('cats ^'); 
     $q.all(cats.data.map(function (cat) { 
      var d = $q.defer(); 

      console.error(cat.name); 

      WordPress.getLatestPostOfCategory(cat.id) 
      .then(function (post) { 

       console.debug(post.data.title.rendered); 

       WordPress.getMediaById(post.data.featured_media) 
       .then(function (media) { 

        console.log(media.data.source_url); 

        cat.firstPost = {}; 
        cat.firstPost.id = post.data.id; 
        cat.firstPost.title = post.data.title.rendered; 
        cat.firstPost.content = post.data.content.rendered; 

        if (cat.firstPost.title.length > 50) { 
        cat.firstPost.title = cat.firstPost.title + '...'; 
        } 

        if (cat.firstPost.content.length > 70) { 
        cat.firstPost.content = cat.firstPost.content.substr(0, 60) + '...'; 
        } 
        cat.firstPost.thumbnail = media.data.source_url; 
        d.resolve(cat); 
       }, function (err) { 
        console.error(angular.toJson(err)); 
       }); 
      }); 

      return d.promise; 
     })).then(function (cats) { 
      console.log('Loaded all articles and for main page.'); 
      $scope.homeCategories = cats; 
      Loading.hide(); 
     }); 
     }); 
    }); 
}); 

コントローラに問題はありますか? P.P.私もすべてのWordPressのサービス関数をデバッグし、うまく動作し、必要なデータを提供します。

EDIT:

時にはそれが永遠にロードするとき、私はconsole.error(cat.name);デバッグメッセージのみ3つのメッセージをログに記録します参照してください。しかし、まだ次の機能に進む...

+0

は[繰延アンチパターン](http://stackoverflow.com/q/23803743/1048572)を避け、すべてのあなたの問題を離れて行くでしょう! (ヒント:あなたは配列にいくつかの約束を永久に残している) – Bergi

+0

@Bergi私に例を挙げてみてください。私は何時間も努力しています... –

+0

非常に何かをする*あらゆる*関数から常に約束を返します。特に 'then'コールバック。 – Bergi

答えて

0

これは、私がBergiのアドバイスによって解決した方法です。助けのための

出典:Promise anti pattern by Gorgi Kosev (bluebird)

var categories = []; 

    function sort() { 
     return WordPress.getAllCategories() 
     .then(function (cats) { 
      console.log('thens'); 
      return $q.all(cats.data.map(function (cat) { 
      console.info('cat: ' + cat.name); 

      var category = {}; 
      category.name = cat.name; 

      return WordPress.getLatestPostOfCategory(cat.id) 
       .then(function (post) { 

       var post = post.data; 
       category.post = {}; 
       category.post.id = post.id; 
       category.post.title = post.title.rendered; 
       category.post.content = post.content.rendered; 

       console.log('ID: ' + category.post.id + ', title: ' + category.post.title); 

       return WordPress.getMediaById(post.featured_media); 
       }).then(function (media) { 
       category.post.thumbnail = media.data.source_url; 

       categories.push(category); 
       console.log('Pushed category "' + category.name + '"'); 
       }); 
      })); 
     }, function (err) { 
      console.error('ERR1'); 
      console.error(angular.toJson(err)); 
     }); 
    } 

    sort() 
     .then(function() { 
     console.info('LOADED ALL CATEGORIES'); 
     $scope.categories = categories; 
     }, function (err) { 
     console.error('err:' + angular.toJson(err)); 
     }); 
関連する問題