christopherthielen / ui-router-extras

THIS PROJECT IS NO LONGER MAINTAINED -- Extras for UI-Router for AngularJS. Sticky States (a.k.a. parallel states), Deep State Redirect (for tab-like navigation), Future States (async state definition)
http://christopherthielen.github.io/ui-router-extras/
MIT License
917 stars 211 forks source link

"Freeze" a previous state? #287

Closed viztastic closed 8 years ago

viztastic commented 8 years ago

Hi,

This is my use case:

Currently I tried to implement this by way of:

if($previousState.get().state.name != (B || C || D)) {
            $previousState.memo("menucaller");
}

With the hope that the "menucaller" memo will only save states of anything but those I want to exclude.. but it seems to not be the case.

Is there a better way of excluding specific states from registering in $previousState ?

christopherthielen commented 8 years ago

Is there a better way of excluding specific states from registering in $previousState ?

No, there is no way to get $previousState to do what you want, sorry. I suggest doing it yourself. Here is an example decorating your state definitions with some custom data, then keeping track and exposing a simple API.

// Decorate the state definitions
.state('stateA', {
  data: { secure: true }
});

module.factory('SecureStates', function($rootScope, $state) {
  // Update service.previous whenever they go to a secure state
  $rootScope.$on('$stateChangeSuccess', function(evt, toState, toParams) {
    if (toState.data.secure) service.previous = { state: toState, params: toParams };
  });

  var service = { 
    previous: null,
    goPrevious: function() { 
      if (service.previous) $state.go(service.previous.state, service.previous.params); 
    }
  };

  return service;
});

module.controller('LoginController', function(SecureStates, $scope, AuthService) {
  $scope.login = function(user, pass) {
    AuthService.authenticate(user, pass).then(SecureStates.goPrevious)(;
  } 
});