2015-06-12 107 views
0

我想根據用戶最近一次訪問子路由動態加載路由。最近的訪問被保存並使用工廠進行檢索。目前我正在使用onEnter,但是會導致父路由加載,然後發生第二次加載以加載子路由。如果需要的信息存在,是否有辦法阻止第二次加載並直接進入子路徑?ui路由器onEnter替代

(function() { 
    'use strict'; 

    angular 
     .module('curation') 
     .config(routesConfig); 

    routesConfig.$inject = ['$stateProvider']; 
    /** 
    * Route Configuration to establish url patterns for the module 
    * @param {Function} $stateProvider 
    */ 
    function routesConfig($stateProvider) { 
     // Curation state routing 
     $stateProvider. 
     state('site.curation', { 
      url: '/curation', 
      templateUrl: 'modules/curation/views/curation.client.view.html', 
      resolve: { 
       publicFronts: function($stateParams, frontsService) { 
        return frontsService.getPublicFronts($stateParams.siteCode); 
       }, 
       authoringTypes: function(assetMetadata) { 
        return assetMetadata.authoringTypes().then(function(value) { 
         return value.data.authoringTypes; 
        }); 
       } 
      }, 
      onEnter: function($state, $timeout, userSitePreferences, publicFronts) { 
       $timeout(function() { 
        var recentFront = _.find(publicFronts, { 
         Id: userSitePreferences.curation.recentFrontIds[0] 
        }); 
        if (recentFront) { 
         $state.go('site.curation.selectedFront.selectedLayout', { 
          frontId: recentFront.Id, 
          layoutId: recentFront.LayoutId 
         }); 
        } 
       }); 
      } 
     }); 
    } 
})(); 

回答

0

嘗試「ui-router-extras」的「深度狀態重定向」。

http://christopherthielen.github.io/ui-router-extras/#/dsr

嘗試這樣的事情...

$stateProvider. 
    state('site.curation', { 
     url: '/curation', 
     templateUrl: 'modules/curation/views/curation.client.view.html', 
     resolve: { 
      publicFronts: function($stateParams, frontsService) { 
       return frontsService.getPublicFronts($stateParams.siteCode); 
      }, 
      authoringTypes: function(assetMetadata) { 
       return assetMetadata.authoringTypes().then(function(value) { 
        return value.data.authoringTypes; 
       }); 
      } 
     }, 
     deepStateRedirect: { 
      default: { 
       state: 'site.curation.selectedFront.selectedLayout' 
       params: { 
        frontId: 'defaultId', 
        layoutId: 'defaultLayout' 
       } 
      }, 
      params: true, 
      // I've never tried to generate dynamic params, so I'm not sure 
      // that this accepts DI variables, but it's worth a try. 
      fn: function ($dsr$, userSitePreferences, publicFronts) { 
       var recentFront = _.find(publicFronts, { 
        Id: userSitePreferences.curation.recentFrontIds[0] 
       }); 
       if (recentFront) { 
        return { 
         state: $dsr$.redirect.state, 
         params: { 
          frontId: recentFront.Id, 
          layoutId: recentFront.LayoutId 
         } 
        }; 
       } else { 
        return false; 
       } 
      }); 
     } 
    }); 
+0

你應該張貼此鏈接的內容,所以如果去不可,其內容還在這裏。 – gustavohenke

+0

我用一些示例代碼更新了我的答案 –

+0

看起來userSitePreferences不再被這種方法識別。 :(感謝您的建議@JeffFairley –