ui-router icon indicating copy to clipboard operation
ui-router copied to clipboard

RFC: Routing to Angular 1.5 .component()

Open christopherthielen opened this issue 10 years ago • 45 comments

I'd like to solicit some feedback from the community about routing to ng1 .component()s.

Backstory

We recently added support for $scope.$resolve to the master and legacy branches. After the next release, you will be able to route to components, and access resolved data using a "component template", similar to ngRoute. Here's a proof of concept: http://plnkr.co/edit/mG9W1q6B4CCHI6QEfKSJ

In angular2, however, we cannot use "component templates", and instead will be routing to a component class. In ui-router-ng2, you will supply the component's class in a state definition, probably something like this:

import {FooComponent} from "./fooComponent.js";
import {UIRouter, StateRegistry} from "ui-router-ng2";

let stateRegistry = uirouter.stateRegistry; // uirouter instance comes from somewhere
stateRegistry.state({
  name: 'foo',
  component: FooComponent, // Reference to the FooComponent class
  url: '/foo/:fooId'
}

I would like both ui-router for ng1 and for ng2 to have a similar look and feel when routing to components. Because of that, I'm considering allowing component: in a state definition for ui-router 1.0 for ng1.

There are a few mechanisms I've been tossing around as options for declaring a state's view should route to a component, and how to provide resolved data to that component. Note that we can't use constructor injection into a .component() to provide resolve data like we would for a normal ui-router template/controller combo, because we can not instantiate and manage the component lifecycle ourselves.


Given this angular 1.5 .component()

app.component('myComponent', {
  templateUrl: 'myComponent.html',
  controller: 'MyController',
  bindings: { input1: '<', input2: '<' }
});

Options

Option 1: Keep it as-is. Use a template: and access data using $resolve.
$stateProvider.state('foo', {
  template: '<my-component input1="$resolve.foo" input2="$resolve.bar"></my-component>',
  url: '/foo/:fooId/:barId',
  resolve: {
    foo: ($stateParams, FooService) => FooService.get($stateParams.fooId),
    bar: ($stateParams, BarService) => BarService.get($stateParams.barId)
  } 
});
Option 2: Route to the component by name.

Require each component bindings to match a resolve name

$stateProvider.state('foo', {
  component: 'myComponent',
  url: '/foo/:fooId/:barId',
  resolve: {
    // input1 and input2 are same names as myComponent `bindings`
    input1: ($stateParams, FooService) => FooService.get($stateParams.fooId),
    input2: ($stateParams, BarService) => BarService.get($stateParams.barId)
  } 
});
Option 3: Route to the component by name + bindings mapping string

Allow resolve names to be mapped to component bindings using a DSL similar to ui-sref The use case is that resolves may come from parent states, so may need to be mapped to the component bindings

$stateProvider.state('foo', {
  component: 'myComponent({ input1: foo, input2: bar })',
  url: '/foo/:fooId/:barId',
  resolve: {
    foo: ($stateParams, FooService) => FooService.get($stateParams.fooId),
    bar: ($stateParams, BarService) => BarService.get($stateParams.barId)
  } 
});
Option 4: Route to the component by name + bindings mapping object

Allow resolve names to be mapped to component bindings using an object Again, the use case is that resolves may come from parent states, so may need to be mapped to the component bindings

$stateProvider.state('foo', {
  component: 'myComponent',
  bindings: { input1: "foo", input2: "bar" },
  url: '/foo/:fooId/:barId',
  resolve: {
    foo: ($stateParams, FooService) => FooService.get($stateParams.fooId),
    bar: ($stateParams, BarService) => BarService.get($stateParams.barId)
  } 
});
Option 5: Route to the component by name; Supply resolves via ui-view controller

For this option, the component has to be aware that it's inside of a ui-view. It requires the ui-view controller and accesses the resolves from there. The bindings object is not used.

app.component('myComponent', {
  templateUrl: 'myComponent.html',
  controller: function() {
    this.$onInit = function() {
      this.input1 = this.uiView.$resolve.foo;
      this.input2 = this.uiView.$resolve.bar;
    }
  },
  require: { uiView: '^uiView' }
});

$stateProvider.state('foo', {
  component: 'myComponent',
  url: '/foo/:fooId/:barId',
  resolve: {
    foo: ($stateParams, FooService) => FooService.get($stateParams.fooId),
    bar: ($stateParams, BarService) => BarService.get($stateParams.barId)
  } 
});

Option 6: Your option

christopherthielen avatar Mar 17 '16 23:03 christopherthielen

I like option 2. It aligns more with how the components are dynamically loaded in Angular 2. How would these resolved values get set for the state component?

brandonroberts avatar Mar 18 '16 02:03 brandonroberts

Option 1

This is what i'd go for. The UI router API is pretty comprehensive already so I wouldn't feel comfortable adding more things.

Option 2

I feel like there would be too much magic going on, especially when someone else picks up the code and doesn't know that resolves are autobound to the component's bindings

Option 3

Not a fan of micro DSLs unless they're unavoidable

Option 4

This looks to me like a better version of Option 2 but I still prefer Option 1

Option 5

Least favourable option in my opinion as it ties a component to UI router which renders it more or less unusable in other contexts which is the idea of a component

timjacobi avatar Mar 18 '16 08:03 timjacobi

I'm mostly leaning towards options 2 and 4. My thought would be to default to the syntax of option 2 but provide the bindings from option 4 as a mapping syntax for when it might be necessary.

luaks avatar Mar 18 '16 12:03 luaks

Option 2, and it is (should) be compatible (extensible) with Option 4.

rogerpadilla avatar Mar 18 '16 14:03 rogerpadilla

How would these resolved values get set for the state component?

@brandonroberts much like the plunker above (see: "routeToComponent.js")

  • Process the resolves (async) during the transition.
  • Inspect the component's DDO for bindToController values
  • Build a ui-router template/controller for the view.
    • The controller instance gets the resolved values as $resolve
    • The generated template is a "component template" such as <my-component input1="::$ctrl.$resolve.input1"></my-component>
  • Angular compiles the template and one-time binds the resolve data to the directive

christopherthielen avatar Mar 18 '16 15:03 christopherthielen

@luciano-spinn3r I'm having trouble understanding all of your points, but it sounds like you are advocating for a drastic architecture change, along the lines of the component router.

That's not on the table here; ui-router will continue to use the state tree abstraction to define the application structure and data retrieval/lifecycle. Components are applied as views.

Feel free to correct me if I missed some nuance of your suggestion. However, I don't want to derail the initial RFC.

christopherthielen avatar Mar 18 '16 15:03 christopherthielen

I was laking this functionality, as I'm looking forward to achieve a full angular app architecture based on routable components (just to test it vs a classical one) and balance the benefits and disadvantages of working with this flow.

I'm my opinion, option 2 as default with optional 4 (more flexible, would override default - good point on parent resolves!) should be the way.

Thanks for all and keep up the good work!

PabloHidalgo avatar Mar 18 '16 16:03 PabloHidalgo

Option 2 looks good to me

eddiemonge avatar Mar 18 '16 16:03 eddiemonge

I think option 4 looks the best, with sensible default (bindings can be omitted with a default to an empty object).

wesleycho avatar Mar 18 '16 17:03 wesleycho

with sensible default (bindings can be omitted with a default to an empty object).

Do you mean you prefer by default, that no automatic bindings would occur like option 2?

christopherthielen avatar Mar 18 '16 17:03 christopherthielen

Yup

wesleycho avatar Mar 18 '16 17:03 wesleycho

option 2 as default with optional 4

DmitryEfimenko avatar Mar 18 '16 19:03 DmitryEfimenko

1 should be supported, and 4 would be nice as an addition.

felixfbecker avatar Mar 19 '16 18:03 felixfbecker

My vote goes to option 2.

gaui avatar Mar 20 '16 14:03 gaui

I think 1 should be supported just to be related to the ngRoute syntax. However, I like 2 as the default. It makes sense that you can provide the bindings through a resolve, though it is possible that a binding might not be required, so it should not get upset by not providing a binding and default to null.

gnomeontherun avatar Mar 22 '16 01:03 gnomeontherun

Done... options 1, 2, and 4 are supported.

This works for:

  • angular 1.5 .component()
  • angular 1.3+ and Todd Motto's .component() polyfill
  • angular 1.3+ bindToController directive
  • angular 1.2+ directive with scope: { foo: '=' } bindings

This code works by decorating states that have component: 'foo' with a templateProvider that builds a template that looks something like <foo my-data="::$resolve.myData"></foo>

christopherthielen avatar Mar 27 '16 19:03 christopherthielen

Nice work!. All seems to be fine fine with Angular 1.5 except it seems $stateChangeError& $stateChangeStartno longer fires. Please note that I have imported stateEvents to project as well. I am using 1.0.0-alpha.3 of ui-router. However $transitions.onError etc fires as expected.

samithaf avatar Apr 28 '16 11:04 samithaf

@samithaf did you add the angular module dependency to "ui.router.events" as well?

christopherthielen avatar Apr 28 '16 21:04 christopherthielen

hey @christopherthielen thanks for getting back and the hint. Wonder module name is ui.router.state.events? I just checked the source of stateEvents.js. When I add the ui.router.state.events as the dependancy everything started working. One request, could you please export name of the Angular module in stateEvents.js? Then people can do something like,

import stateEvents from 'angular-ui-router/release/stateEvents';

.module('my.app', [
stateEvents

samithaf avatar Apr 28 '16 21:04 samithaf

Yes! Whoops. On Thu, Apr 28, 2016 at 4:56 PM Samitha Fernando [email protected] wrote:

hey @christopherthielen https://github.com/christopherthielen thanks for getting back. Wonder module name is ui.router.state.events? I just checked the source of soruceEvents.js

— You are receiving this because you were mentioned.

Reply to this email directly or view it on GitHub https://github.com/angular-ui/ui-router/issues/2627#issuecomment-215574784

christopherthielen avatar Apr 28 '16 22:04 christopherthielen