Permalink
Browse files

Use the router to generate redirect urls, when possible.

  • Loading branch information...
1 parent 887ea26 commit f4e4bc5853e62a63ef64935944a3d844d9179bb6 @david-mitchell david-mitchell committed Mar 13, 2012
Showing with 15 additions and 3 deletions.
  1. +1 −0 lib/app.js
  2. +10 −1 lib/base_controller.js
  3. +4 −2 lib/routers/regexp_router.js
View
@@ -402,6 +402,7 @@ var App = function () {
controller.accessTime = accessTime;
if (typeof controller[params.action] == 'function') {
+ controller.app = self;
controller.request = reqObj;
controller.response = resp;
controller.method = method;
@@ -595,7 +595,16 @@ controller.BaseController.prototype = new (function () {
if (typeof target == 'string') {
url = target;
}
- else {
+ else if (typeof this.app.router.url == 'function') {
+ if (this.name && !target.controller)
+ target.controller = this.name;
+ if (this.params.format && !target.format)
+ target.format = this.params.format;
+
+ url = this.app.router.url(target);
+ }
+
+ if (!url) {
var contr = target.controller || this.name;
var act = target.action;
var ext = target.format || this.params.format;
@@ -199,6 +199,9 @@ var Router = function () {
this.url = function (params) {
var url = false;
+ // attempt the stringification with defaults mixed in
+ params = geddy.mixin({controller:'Application', action:'index' }, params);
+
// iterate through the existing routes until a suitable match is found
for (var i in this.routes) {
// do the controller & acton match?
@@ -210,8 +213,7 @@ var Router = function () {
this.routes[i].params.action != params.action) {
continue;
}
- // attempt the stringification with defaults mixed in
- params = geddy.mixin({controller:'Application', action:'index' }, params);
+
url = this.routes[i].stringify(params);
if (url) {
break;

0 comments on commit f4e4bc5

Please sign in to comment.