baseprime / grapnel

The smallest JavaScript router with named parameters, HTML5 pushState, and middleware support
http://grapnel.js.org
468 stars 40 forks source link

Navigation without triggering route matching #57

Closed thunderkid closed 6 years ago

thunderkid commented 8 years ago

Great library. Thanks for your work! Is there a way to set the url hash without triggering the route handlers?

I've tried path(newPath), but that triggers them just like navigate(newPath). I can imagine doing it with preventDefault() in a pretty hackish way - I'd need to have an ignoreMatching flag of some sort, but turning this on and off safely seems rather fragile. (eg, I'd need to clear the flag whether or not a match was found, and worry about event orders.)

Is there a more efficient way to do it? Some other libraries just have a silently option for navigate().

droplab commented 8 years ago

+1 on this question as well. thanks

baseprime commented 8 years ago

There currently isn't a way to do this. But you can use preventDefault() as a workaround. The window always fires the hashchange or popstate events, but you can specify whether or not you want the router to continue. @thunderkid @droplab

thunderkid commented 8 years ago

Do you mean something like this as a workaround?

    this.router.on('hashchange', (event) => this.onPopstate(event, this.router.path()));

then

    onPopstate(event, path: string) {
        if (path == pathThatIJustSet)
            event.preventDefault();
 }

when I try this I get that event is undefined. Thanks.

adam-lynch commented 7 years ago

@thunderkid maybe they meant this: https://github.com/baseprime/grapnel/issues/46#issuecomment-129550677