2014-02-12 16 views
2

萬物的偉大工程,在我的應用程序,但角JS - 停止控制器的活動(http請求壽)開關控制器(網站頁面)時

我有一個奇怪的問題:

如果我在/channels和我啓動pagination分頁大約需要5秒結束,

因此,如果在此期間,該分頁加載,我改變控制器和視圖瀏覽另一個應用頁面,當分頁已經完成了他的過程中,我得到[R編輯到/頻道:O

這怎麼可能?以及如何避免這種情況?

這是一段簡單的代碼涉及:

$routeProvider 
     .when('/',{ 
      templateUrl:'views/home/index.html', 
      controller:'HomeController' 
     }) 
     .when('/auth/login',{ 
      templateUrl:'views/auth/login.html', 
      controller:'UsersController' 
     }) 
     .when('/auth/signup',{ 
      templateUrl:'views/auth/signup.html', 
      controller:'UsersController' 
     }) 
     .when('/channels',{ 
      templateUrl:'views/channels/index.html', 
      controller:'BrowseController' 
     }) 
     .when('/channels/search',{ 
      templateUrl:'views/channels/index.html', 
      controller:'BrowseController', 
      reloadOnSearch: false 
     }) 
     .when('/channels/create',{ 
      templateUrl:'views/channels/create.html', 
      controller:'ChannelsController' 
     }) 
     .when('/users',{ 
      templateUrl:'views/users/index.html', 
      controller:'UsersController' 
     }) 
     .otherwise({ 
      redirectTo:'/' 
     }); 

    app.controller('BrowseController',['$rootScope','$scope','$route','$routeParams','$http','$location','ngDialog', function($rootScope,$scope,$route,$routeParams,$http,$location,ngDialog) { 
      $scope.layout.loading = true; 
     $scope.pagination = { 
      "keywords":$routeParams.keywords, 
      "offset":$routeParams.offset , 
      "genre":$routeParams.genre , 
      "last_item":0, 
      "results": [] 
     } 

     $scope.redirect_pagination = function(_map){ 

      $location.path('channels/search').search(_map); 

     } 

     $scope.paginate = function(_offset,_genre,_keywords){ 
      $rootScope.layout.loading = true; 
      if(!_offset){ 
       _offset = 1; 
      } 

      $http({ 
       method:'GET', 
       url:$scope.config.app_ws+'get/channels/', 
       params:{ 
        offset:_offset, 
        genre:_genre, 
        keywords: _keywords 
       } 
      }).success(function(response){ 
      $scope.pagination.results.push(response); 
      $scope.pagination.last_item = $scope.pagination.results.length; 
      if($scope.config.app_url !== '/channels'){ 
       $scope.redirect_pagination({ 
        'offset':_offset, 
        'genre':_genre, 
        'keywords':_keywords 
       }); 
      } 

      $scope.pagination.offset = _offset * 2 ; 
      $rootScope.layout.loading = false; 
      }).error(function(status, response){ 
       alert('opsss'); 
      }); 
     } 
     //default pagination or by url params 

     $scope.paginate($scope.pagination.offset,$scope.pagination.genre,$scope.pagination.keywords); 

    }]) 

其他的東西:

$rootScope.$on('$routeChangeStart', function(newRoute, oldRoute) { 
$rootScope.layout.loading = true; 

//Make app scroll to top by default while navigating or use #anchor in url like http://sitse.com/someroute/?scrollTo=<:id_element:> to make it scroll to that element :id: 
$location.hash($routeParams.scrollTo); 
$anchorScroll(); 
//Close dialog every route change 
ngDialog.close(); 

}); 
$rootScope.$on('$routeChangeError', function() { 
//hide loading gif 
$rootScope.layout.loading = false; 
}); 
$rootScope.$on('$routeChangeSuccess', function() { 
//Animate only some route 
if($rootScope.layout.routes.indexOf($rootScope.config.app_url) == -1){ 
    $rootScope.layout.animation = false; 
}else{ 
    $rootScope.layout.animation = true; 
} 
//hide loading gif 
$rootScope.layout.loading = false; 

}); 

}]); 
+0

只是爲了澄清,當用戶請求一個特定的路線,要對它們進行重定向,直到另一個請求已完成,然後重定向他們回來? –

+0

@MattWay不,不,我想避免那:)因爲實際上我得到重定向,如果分頁ajax沒有完成,iswitched控制器(網站頁面) – sbaaaang

+0

聽起來像你可能想做一個'決心'。 http://docs.angularjs.org/api/ngRoute.$routeProvider –

回答

1

您需要一種方法來從重定向用戶返回停止$location.path()。我認爲實現這一目標的方法之一是設置一個標誌,像這樣:

app.controller('BrowseController', [..., function($scope, ...) { 
    ... 

    var paginationInProgress = false; 

    // Cancel pagination if route has been changed 
    $scope.$on('$routeChangeStart', function() { 
     paginationInProgress = false; 
    }); 

    $scope.redirect_pagination = function(_map){ 
     if (paginationInProgress) { 
      $location.path('channels/search').search(_map); 
      paginationInProgress = false; 
     } 
    }; 

    $scope.paginate = function(_offset,_genre,_keywords){ 
     ... 
     paginationInProgress = true; 
    }; 
}]); 
+0

這是一個聰明的解決方案謝謝我會等待更好的解決方案,如果不是,我會接受你自己的:) – sbaaaang

+0

@sbaaaang沒問題;) –