olivernn / davis.js

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

Replacing a parameter in the current route #53

Open scottkf opened 12 years ago

scottkf commented 12 years ago

Is there an easy way to accomplish this without reusing the code in the prototype.run function? It doesn't seem like it, but I could be blind! I'm trying to programmatically redirect something, but change a parameter.

olivernn commented 12 years ago

A request has a redirect method already see the docs.

I'm not entirely sure what you mean by "programmatically redirect something, but change a parameter", do you want to re-run the same route but add a parameter if it is missing? This could be achieved with some route middleware, e.g.

var paramChanger = function (req, next) {
  req.params.foo += 1
  next(req)
}

this.get('/somepath', paramChanger, function (req) {
  // do whatever with req.params.foo now
})

More on this in the docs. This wouldn't re-run the route but do the massaging of the params before your route is run…