How to detect if a route is autogenerated like an 'index' route?


#1

So, I’m trying to figure out if a route was autogenerated versus an actual route the developer created. Is this possible?

I went through the entire tree of the object and can’t seem to find anything that would remotely let me know if it was auto-generated except for finding a property called template that’s null.


#2

I could swear there used to be an isGenerated property on either the route or controller but can’t find any reference to it

If you explain what you’re trying to accomplish then there might be another way


#3

Although this question is old, I would like to propose my solution.

When a controller is autogenerated, its toString() method returns “(controller $ {controllerName} generated)”. We can therefore test this value to know if it is an autogenerated controller :slight_smile:

To define an isAutoGenerated boolean property in all Controllers, we can define an initializer like this:

// app/initializers/is-autogenerated-controller.js
import Controller from '@ember/controller';

export function initialize(/* application */) {
  Controller.reopen({
    init() {
      this._super(...arguments);
      this.isAutoGenerated = this.toString().startsWith('(generated');
    }
  });
}

export default {
  name: 'add isAutoGenerated property in Controller',
  initialize
};