canjs / can-route

Observable front-end application routing for CanJS.
https://canjs.com/doc/can-route.html
MIT License
10 stars 4 forks source link

Add docs for listening to any route change #230

Open chasenlehara opened 5 years ago

chasenlehara commented 5 years ago

Inspired by @pmoscuzza-tc’s question on Slack:

I created this event listener to listen to changes to the route and then determine what controller to go to.

route.matched.on('change', function(ev, attr) {
    thisController[(attr) ? `${attr} route` : 'route'](route.data);
});

Here is an example of the issue. If we tried to go to the path /cars/details from /cars this will fire that event listener and work. The route for cars would be product and for details that would be action. Now if we then tried to go to /cars/color from /cars/details this will not fire that event listener.

This only seems to fire if the actual route changes, not the path. Is there something I can do that can also detect a path change? I had tried using route.data.on but that did not seem to work.

Someone else asked a similar question recently too.

It looks like we added a private __url event that can be listened to: https://github.com/canjs/can-route/pull/80#issuecomment-304069436

I’d like us to decide if we want to document that (or make another official API), or document another way of accomplishing the same result (as a getter in a DefineMap, with can-value, etc.).