Skip to content

Instantly share code, notes, and snippets.

@cognivator
Last active August 25, 2016 19:33
Show Gist options
  • Save cognivator/ad61c75e80985d0b44fa40e9cc8e4bb9 to your computer and use it in GitHub Desktop.
Save cognivator/ad61c75e80985d0b44fa40e9cc8e4bb9 to your computer and use it in GitHub Desktop.

Revisions

  1. cognivator revised this gist Aug 25, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ui-router controller instantiation.md
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ $stateProvider
    });
    ```
    Note the lack of urls for the child states. This prevents directly navigating to any child state.
    Also note that `$deepStateRedirect` (part of [ui-router-extras|https://christopherthielen.github.io/ui-router-extras/#/home] would allow a configuration like the following to automatically navigate from the parent `login` state to the child `login.tos` state,
    Also note that `$deepStateRedirect` (part of [ui-router-extras](https://christopherthielen.github.io/ui-router-extras/#/home) would allow a configuration like the following to automatically navigate from the parent `login` state to the child `login.tos` state,
    ```
    $stateProvider
    .state('login', {
    @@ -45,7 +45,7 @@ The problem with using `$deepStateRedirect` in this case is it also allows a bro
    Without `$deepStateRedirect`, therefore, it is expected that the `LoginController` will be responsible for issuing `$state.go('login.tos');` whenever the `login` state is activated.

    ## Testing
    In order to test such a controller-mitigated state transition, the `LoginController` must be instantiated by ui-router during testing. The following test harness involves compiling a simple HTML snippet that provides the `<div ui-view />` tag required by ui-router. (Inspired by [ui-router unit tests|])
    In order to test such a controller-mitigated state transition, the `LoginController` must be instantiated by ui-router during testing. The following test harness involves compiling a simple HTML snippet that provides the `<div ui-view />` tag required by ui-router. (Inspired by [ui-router unit tests](https://github.com/christopherthielen/ui-router-extras/blob/master/test/transitionSpec.js))

    ```
    it('should chain from the parent state to the proper child state', function() {
  2. cognivator revised this gist Aug 25, 2016. 1 changed file with 28 additions and 15 deletions.
    43 changes: 28 additions & 15 deletions ui-router controller instantiation.md
    Original file line number Diff line number Diff line change
    @@ -5,34 +5,47 @@ You have a parent state that should always navigate to a child state, but _witho
    ## Configuration
    This is an example ui-router state hierarchy that defines a series of states used for login pages.
    ```
    $stateProvider([
    {state: 'login',
    config: {
    $stateProvider
    .state('login', {
    url: '/login',
    templateUrl: 'app/login/login.html',
    controller: 'LoginController',
    controllerAs: 'loginVm'
    }
    },
    {state: 'login.tos',
    config: {
    })
    .state('login.tos', {
    templateUrl: 'app/login/tos.html',
    controller: 'TosController',
    controllerAs: 'tosVm'
    }
    },
    {state: 'login.credentials',
    config: {
    })
    .state('login.credentials', {
    templateUrl: 'app/login/credentials.html',
    controller: 'credentialsController',
    controllerAs: 'credVm'
    }
    }]);
    });
    ```
    It is expected that the `LoginController` will be responsible for issuing `$state.go('login.tos');` whenever the `login` state is activated.
    Note the lack of urls for the child states. This prevents directly navigating to any child state.
    Also note that `$deepStateRedirect` (part of [ui-router-extras|https://christopherthielen.github.io/ui-router-extras/#/home] would allow a configuration like the following to automatically navigate from the parent `login` state to the child `login.tos` state,
    ```
    $stateProvider
    .state('login', {
    url: '/login',
    templateUrl: 'app/login/login.html',
    controller: 'LoginController',
    controllerAs: 'loginVm',
    deepStateRedirect: {
    default: {
    state: 'login.tos'
    }
    }
    })
    ...
    ```
    The problem with using `$deepStateRedirect` in this case is it also allows a browser refresh, for instance, to remain in any child state like `login.credentials`, which may not be desirable. (The same might be true of any sequential wizard, where navigating away and then back to the wizard should always start at the beginning.)

    Without `$deepStateRedirect`, therefore, it is expected that the `LoginController` will be responsible for issuing `$state.go('login.tos');` whenever the `login` state is activated.

    ## Testing
    In order to test such a state transition, the `LoginController` must be instantiated by ui-router during testing. The following test harness involves compiling a simple HTML snippet that provides the `<div ui-view />` tag required by ui-router.
    In order to test such a controller-mitigated state transition, the `LoginController` must be instantiated by ui-router during testing. The following test harness involves compiling a simple HTML snippet that provides the `<div ui-view />` tag required by ui-router. (Inspired by [ui-router unit tests|])

    ```
    it('should chain from the parent state to the proper child state', function() {
  3. cognivator revised this gist Aug 25, 2016. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion ui-router controller instantiation.md
    Original file line number Diff line number Diff line change
    @@ -29,6 +29,10 @@ $stateProvider([
    }
    }]);
    ```
    It is expected that the `LoginController` will be responsible for issuing `$state.go('login.tos');` whenever the `login` state is activated.

    ## Testing
    In order to test such a state transition, the `LoginController` must be instantiated by ui-router during testing. The following test harness involves compiling a simple HTML snippet that provides the `<div ui-view />` tag required by ui-router.

    ```
    it('should chain from the parent state to the proper child state', function() {
    @@ -40,4 +44,6 @@ it('should chain from the parent state to the proper child state', function() {
    expect($state.is('login.terms')).to.be.true;
    };
    ```
    ```
    Note the use of `$httpBackend` to mock responses to any request the `LoginController` may make. If your controller doesn't make such requests, this mock isn't necessary.

  4. cognivator created this gist Aug 25, 2016.
    43 changes: 43 additions & 0 deletions ui-router controller instantiation.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    ## Scenario
    You're using ui-router to manage Angular 1.x routing states.
    You have a parent state that should always navigate to a child state, but _without_ relying on ui-router-extras like `$deepStateRedirect`. This is to avoid deep linking into the child state. Some logins need to operate this way, for instance, so the Terms of Service acknowledgement cannot be skipped.

    ## Configuration
    This is an example ui-router state hierarchy that defines a series of states used for login pages.
    ```
    $stateProvider([
    {state: 'login',
    config: {
    url: '/login',
    templateUrl: 'app/login/login.html',
    controller: 'LoginController',
    controllerAs: 'loginVm'
    }
    },
    {state: 'login.tos',
    config: {
    templateUrl: 'app/login/tos.html',
    controller: 'TosController',
    controllerAs: 'tosVm'
    }
    },
    {state: 'login.credentials',
    config: {
    templateUrl: 'app/login/credentials.html',
    controller: 'credentialsController',
    controllerAs: 'credVm'
    }
    }]);
    ```

    ```
    it('should chain from the parent state to the proper child state', function() {
    $httpBackend.when('GET', function() {return true;}).respond(200);
    $compile('<div> <div ui-view/> </div>')($rootScope);
    $state.go('login');
    $rootScope.$digest();
    expect($state.is('login.terms')).to.be.true;
    };
    ```