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

Simple Change: Make resolveRoute a class member of UniversalRouter instead of privately scoped #205

Open
1 of 3 tasks
joezappie opened this issue Dec 15, 2021 · 1 comment

Comments

@joezappie
Copy link

I'm submitting a ...

  • bug report
  • feature request
  • other (Please do not submit support requests here (below))

Feature request

Would there be any backwards compatibility issues with moving the resolveRoute function to be inside the UniversalRouter class? The reason for this is when extending UniversalRouter, you cannot override the resolveRoute function as its a privately scoped function in the UniversalRouter.js file, outside of the UniversalRouter class.

Currently I have my resolveRouter passed in as a parameter, but I have a custom class for my router to add in functionality like history API. Being able to override resolveRouter inside that class would be a cleaner interface, keeping like logic together. I don't see any conflicts with this change and would improve class support. Is there a good reason for this to stay as a private function?

Proposed Change

class UniversalRouter {
    constructor(routes, options) {
         ...
    }

    resolve(pathnameOrContext) {
        ....
        const resolve = this.options.resolveRoute || this.resolveRoute;
        ...
    }
    
    resolveRoute(context, params) {
        if (typeof context.route.action === 'function') {
            return context.route.action(context, params);
        }
        return undefined;
    }
}

Then custom router classes can override it or it can still be passed in as an option.

class MyRouter extends UniversalRouter {
    resolveRoute(context, params) {
        // custom logic
    }
}
@frenzzy
Copy link
Member

frenzzy commented Dec 17, 2021

You can override resolveRoute in your custom class like this:

class YourRouter extends UniversalRouter {
  constructor(routes, options) {
    super(routes, { ...options, resolveRoute: this.resolveRoute })
  }
  resolveRoute(context, params) {
    // custom logic
  }
}

But could you tell us a little about why you need to extend the UniversalRouter class, i.e. what functionality are you missing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants