eslint-plugin-ember icon indicating copy to clipboard operation
eslint-plugin-ember copied to clipboard

Question about recommended way to access a controller from a route given `no-controller-access-in-routes` rule

Open 0xMurage opened this issue 3 years ago • 7 comments

Accessing this.controllerFor() property inside setupController/resetController method in a route class raises a lint error ( no-controller-access-in-routes). Is that the expected behavior and if yes, how can one access another controller in a route class.

I am accessing the controller to setup route transition redirect like this (I could opt to use query params or disable the lint on the affected LOC but is there an option to access another controller in a route)

Screenshot_from_2021-03-09_14-32-38

0xMurage avatar Mar 17 '21 12:03 0xMurage

I have the same doubt. In my case, I am in a child route, and want change the state of some variables in the parent controller. How will be the better way to do this?

rogeraraujo90 avatar Mar 17 '21 15:03 rogeraraujo90

@emonroy could you comment? CC: @rwjblue

Maybe we can improve the no-controller-access-in-routes rule documentation.

bmish avatar Mar 22 '21 21:03 bmish

Also hitting up against this: the rule and the Ember guides for Storing and Retrying Transitions are at odds. 🙃

roomman avatar May 18 '21 14:05 roomman

I am upgrading ember from version 3.20 to 3.24.

The no-controller-access-in-routes rule recommends not using a controller within a route, unless the reference to the controller is in the setupController/resetController hooks, as can be seen in the link below:

https://github.com/ember-cli/eslint-plugin-ember/blob/master/docs/rules/no-controller-access-in-routes.md

However we have some willTransition actions that access the controller, and even the ember documentation, there is an example of willTransition accessing the controller, as can be seen in the link below:

https://api.emberjs.com/ember/3.26/classes/Route/events/willTransition?anchor=willTransition

this eslint rule is correct? If the app needs access to the controller from willTransition, how to handler this question?

doska80 avatar Jul 05 '21 18:07 doska80

I also am wondering how to access the controller in willTransition, I don't see a workaround besides just disabling the rule.

@action willTransition(transition) {
  if (this.currentModel.hasDirtyAttributes) {
    this.controller.transition = transition;
    transition.abort();
  }
}

charlesfries avatar Sep 20 '21 21:09 charlesfries

I would recommend we restore this rule after Learning team has alignment and updated guidance. Leaving this rule as-is will likely push teams to disable it entirely, so I don't think we can simply change the behavior now.

Data points from GH search: https://github.com/search?l=JavaScript&q=no-controller-access-in-routes&type=Code

The unanswered question on the original issue prompting this rule stood out as a flag for me as well: https://github.com/ember-cli/eslint-plugin-ember/issues/896#issuecomment-665283923

manufacturedba avatar Jul 29 '22 06:07 manufacturedba

I recently ran into this as well but the linter error makes it sound like it's broken. It says do not access controller in route outside of setupController/resetController, but I'm inside of those?

Is controllerFor not allowed at all anywhere? Like others have mentioned, the Ember Guides suggest to use it in some cases. This linting rule has me very confused.

image

I also noticed that there's no way to use the allowControllerFor option, eslint complains about the config if you try something like this:

  rules: {
    'ember/no-controller-access-in-routes': {
      allowControllerFor: true,
    },
  },

For the time being I have opted to disable this rule entirely.

bitwolfe avatar May 17 '23 12:05 bitwolfe