2016-04-13 16 views
0

我在我的控制器中使用promise,並且大多數時候它運行良好。但有時它只是永遠加載,並且WordPress.getAllCategories()函數甚至不會被調用。AngularJS /離子承諾 - 有時頁面加載永遠

這是我的控制器: 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.S.我也調試所有的WordPress服務功能,他們工作得很好,並提供所需的數據。

編輯:

有時,當它加載永遠,我看到console.error(cat.name);調試消息只記錄3個消息。但仍然繼續下一個功能...

+0

避免[遞延反模式](http://stackoverflow.com/q/23803743/1048572)和你所有的問題就會迎刃而解! (提示:你會在陣列中留下一些承諾,永遠等待) – Bergi

+0

@Bergi你可以請嘗試給我一個例子嗎?我嘗試了幾個小時...... –

+0

總是'返回'一個承諾,從*每個*異步的功能。特別是'然後'回調。 – 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)); 
     });