Skip to content

[router]: using @inject for ROUTE_DATA #4392

Closed

Description

@cexbrayat

Don't you think that having to use @Inject for ROUTE_DATA is a bit surprising compared to RouteParams which gives us a service? I think this is a very rare case (the only one?) where we can't rely on Type annotation in an app for something provided by the framework. And we obtain an object, whereas RouteParams provides a get method.

constructor(routeParams: RouteParams, @Inject(ROUTE_DATA) routeData) {
  let id = routeParams.get('id');
  let name = ROUTE_DATA.name;
}

A less surprising (and more coherent and friendly in my opinion) would be:

constructor(routeParams: RouteParams, routeData: RouteData) {
  let id = routeParams.get('id');
  let name = routeData.get('name');
}

(this is a follow up issue from #3541)

Activity

  1. cexbrayat commented on Sep 28, 2015

    @cexbrayat
    MemberAuthor
  2. PatrickJS commented on Sep 28, 2015

    @PatrickJS
    Contributor

    The injector allows for any token include types. I agree with @cexbrayat that we should allow the type token RouteData on the merit that it's a valid token. I guess naming conventions should also have ref in the name RouteParamsRef and RouteDataRef. The real problem here is that ROUTE_DATA can only guarantee a shallow copy (since only two types are allowed array|object) as it's unaware of the types you provide. Unless data is a factory

    @RouteConfig([
      {
        path: '/metadata',
        component: MetaDataCmp,
        data() {
          return new AwesomeData('wat');
        }
      }
    ])

    if not RouteData than perhaps RouteConfigRef

    constructor(routeParams: RouteParams, routeConfigRef: RouteConfigRef) {
      let id = routeParams.get('id');
      let name = routeConfigRef.get('data').name;
    }

    or perhaps the bindings in router-outlet is missing bind(ComponentInstruction).toValue(nextInstruction) and RouteConfig needs to include bindings prop. Adding these two seems to be the best option since anyone can create their own conventions

  3. mhevery commented on Sep 29, 2015

    @mhevery
    Contributor

    👍 for having RouteData type so that it can be cleanly injected.

  4. btford commented on Sep 29, 2015

    @btford
    Contributor

    I think I'm convinced a type would be better.

    PR welcome :)

  5. cexbrayat commented on Sep 30, 2015

    @cexbrayat
    MemberAuthor

    @mhevery @btford Thx for your answers.

    Do we agree on a RouteData type that we can inject, and which would offer a getter (as RouteParams does)?

    constructor(routeParams: RouteParams, routeData: RouteData) {
      let id = routeParams.get('id');
      let isAdmin = routeData.get('isAdmin');
    }
    

    If so, I guess a Route data should be a simple object, like:

    new Route({path: '/user/:id', component: UserCmp, data: { isAdmin: true }})
    

    Would that be OK?

  6. cexbrayat commented on Sep 30, 2015

    @cexbrayat
    MemberAuthor

    (I opened a related PR #4428 to start the discussion, feedback welcome)

  7. 0x-r4bbit commented on Sep 30, 2015

    @0x-r4bbit
    Contributor

    @cexbrayat thanks for starting this, I already took a look and it LVGTM!

  8. 6 remaining items

  9. modified the milestone: on Oct 27, 2015
  10. added 2 commits that reference this issue on Oct 27, 2015
    ff3e6cf
  11. angular-automatic-lock-bot commented on Sep 7, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  12. locked and limited conversation to collaborators on Sep 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking changeseffort1: hoursopen for contributionsAn issue that is suitable for a community contributor (based on its complexity/scope).refactoringIssue that involves refactoring or code-cleanup

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions