Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Future States #2739

Closed
amcdnl opened this issue May 11, 2016 · 1 comment
Closed

[Feature] Future States #2739

amcdnl opened this issue May 11, 2016 · 1 comment

Comments

@amcdnl
Copy link
Contributor

amcdnl commented May 11, 2016

1.0 no longer supports future states.

On the flip side, it is easier to swing this in 1.0 without an advanced plugin. I wanted to start the discussion for what my implementation looks like.

This implementation relies on: ng1, oclazyload, systemjs, ui-router 1.0apha4.

const services = [ '$q', '$state', 'cfpLoadingBar',
  'ng2.Compiler', 'ng2.ComponentFactoryRefMap', '$transitions' ];

export const configLaziness = [
  ...services,
  ($q, $state, $ocLazyLoad, compiler, componentFactoryRefMap, $transitions) => {

    function buildState(src) {
      let def = $q.defer();

      System.import(src).then(function(loaded){
        let newModule = loaded;
        if(!loaded.name){
          newModule = loaded.default;
        }

        adapter.compileNg2Components(compiler, componentFactoryRefMap);

        $ocLazyLoad.load(newModule).then(function(){
          cfpLoadingBar.complete();
          def.resolve();
        }, function(err) {
          throw err;
        });
      });

      return def.promise;
    };

    $state.$urlRouter.urlRouterProvider.otherwise(($injector, $location) => {

     /// below few lines sucks and is a bit nieve
      const url = $location.url().replace(/^\/?|\/?$/, ''); 
      const splits = url.split('/');
      const state = routes.find(r => {
        return r.urlPrefix.indexOf(splits[0]) > -1; 
      });

      if(state) {
        buildState(state.src).then(() => {
          $state.$urlRouter.sync();
        });
      } else {
        $state.go('404');
      }
    });

    $state.stateProvider.onInvalid(($to$, $from$, $state) => {
      const name = $to$.name();
      let state = routes.find(r => { return r.stateName === name });

      if(state) {
        var redirect = {
          to: $to$.identifier(),
          toParams: $to$.params(),
          options: $to$.options()
        };

        buildState(state.src).then(() => {
          $state.go(redirect.to, redirect.toParams, redirect.options);
        });
      } else {
        $state.go('404');
      }
    });
  }
];

and the route definitions look like:

[
  {
    "stateName": "apps",
    "urlPrefix": "/apps",
    "src": "app/admin/apps/apps.js"
  }
]

and this is configured like:

import angular from 'angular';
import { configRouting, configLaziness } from 'routing.js';

let app = angular.module('app', [ 'ui.router' ]);
app.config(configRouting);
app.run(configLaziness);

Its also worth noting this supports angular2 lazy loading.

@amcdnl
Copy link
Contributor Author

amcdnl commented Aug 31, 2016

WOO! Have any API Docs yet?

Reference: https://github.com/swimlane/ng1-ng2-webpack-lazy-uirouter

christopherthielen added a commit that referenced this issue Sep 9, 2016
- Retry URL sync by matching state*.url
- Make registration of new states (by the lazyload hook) optional
- Reuse lazy load promise, if one is in progress
- Refactor so ng1-to-ng2 UIRouter provider is implicit
- When state has a .lazyLoad, decorate state.name with `.**` wildcard
Closes #146
Closes #2739
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants