mtrpcic/pathjs

Access params in enter/exit

Closed this issue · 4 comments

Having access to the params hash inside the enter callback would be awesome. I don't think it is possible to access this currently. Correct?

The params appear to be available in the exit callback. I suppose that has something to do with the fact that the route.do_exit and route.action are set in the same manner and the route.do_enter callbacks are set using an array? Would that affect their context so that calling this references the route in the action and do_exit functions but not in the do_enter functions?

I was able to get access to the params by changing the do_enter to work like action and do_exit, that is to say, setting this.do_enter = fn instead of an array of functions. This obviously breaks the feature of being able supply multiple enter callbacks, but for right now a single enter callback works for me.

I didn't think of it yesterday but this morning I was flipping through 'JavaScript: The Good Parts' and I came across the section on function invocation and was reminded of apply. You can call apply on a function and pass in two things: the first is what this should be and the second are the params. This allowed me to pass the current route into the do_enter function which enables this.params[] to work inside the callbacks.

I've accepted the pull request, and this issue is now closed. Thanks for contributing.