olivernn / davis.js

RESTful degradable JavaScript routing using pushState
http://davisjs.com
532 stars 58 forks source link

Adding new routes after app.start() #69

Closed cnizzardini closed 2 years ago

cnizzardini commented 11 years ago

Hello,

I am attempting to add a new route after I've started davis. I do this by adding davis to a parent object which is in the global scope, then later on in my application I called the following code:

var me = this;
Core.davis.get('/retread_templates/view/:id', function(){
    me.view(req.params['id']);
})

But when I click a link that looks like /retread_templates/view/40 the browser natively navigates to that location. Am I not supposed to be doing this? Should I define all routes when I create the initial instance? What is the best practice here?

Great library by the way.

jona10 commented 11 years ago

Hi, I'm in somewhat the same situation as you, wanting to add and remove routes throughout the lifetime of the application. The feature currently doesn't exist in Davis.js right now. I have written a workaround which can be found in the pull request https://github.com/olivernn/davis.js/pull/68 which adds a "destroy" function to the app. The workaround, per say, consists as stopping, destroying, re-adding routes and re-starting the app. Not optimal, I know, but Oliver said that he'd be looking into adding the add/remove feature in the future.

Hope it helps, Jonathan

cnizzardini commented 11 years ago

Oliver, can you at least explain where routes get stored so I can add in this feature myself? Looking at the code it appears routes go into routeCollection but I can't determine where this actually exists using the chrome console.

cnizzardini commented 11 years ago

It appears the following hack may work. It works by stuffing an instance of Davis into the window namespace and then referencing that object later on when you want to add routes. Here's an example:

window.CoreDavis = Davis(function () { // all your this.get() calls here })

then later on somewhere else in your application:

window.CoreDavis.get('/finished_goods/part_map',function(){ alert('omg a route!'); })