Permalink
Browse files

Moved the controller Router interface inside ActiveRouter - less meth…

…ods, less pain
  • Loading branch information...
1 parent 93fe467 commit 70ee989b71bb72e060269833366ea9b99fced57c @fk1blow committed Jun 1, 2011
Showing with 42 additions and 8 deletions.
  1. +35 −3 lib/Router/ActiveRouter.js
  2. +7 −5 lib/Router/Router.js
View
@@ -1,16 +1,48 @@
-var Router = require('./Router');
+var Router = require('./Router'),
+ Request = require('../Http/Request');
var ActiveRouter = Klass({
statics: {
route: null,
- initRouter: function(req) {
- this.route = Router.Request.parseRoute(req.url + '/', true);
+ init: function() {
+ this.route = Router.Request.parseRoute(Request.Factory().getUrl() + '/', true);
},
getRoute: function() {
return this.route;
+ },
+
+ getControllerName: function() {
+ var route_controller = this.getRoute().controller;
+ // This pseudo error handling must go
+ if(!route_controller) {
+ throw new Error('Unable to find a route for:: ' + Request.Factory().getUrl())
+ }
+ return route_controller;
+ },
+
+ getActionName: function() {
+ var route_action = this.getRoute().action;
+ if(!route_action) {
+ throw new Error('Unable to find a route(action) for:: ' + Request.Factory().getUrl())
+ }
+ return route_action;
+ },
+
+ getParams: function() {
+ var route = this.getRoute();
+ var params = {};
+ for(var item in route) {
+ if(item != 'controller' && item != 'action')
+ params[item] = route[item];
+ }
+ return params || null;
+ },
+
+ getUrl: function() {
+ return this.getRoute().current_url;
}
}
View
@@ -1,10 +1,6 @@
-
var Router = {};
-/**
- * @todo move ActiveRoutes to Seek namespace
- */
Router.Base = (function() {
var routes = [];
var request_items = {};
@@ -93,6 +89,7 @@ Router.Request = (function() {
testRoutePath: function(route) {
var regex = this.constructRegex(route);
+
if(regex.test(current_url)) {
return true;
}
@@ -101,10 +98,15 @@ Router.Request = (function() {
constructRegex: function(route) {
var f_r = route.path.replace(/:[\w]+/g, "[\\w]+");
- var regex = new RegExp(route_regexp = '^' + f_r + '\\/$');
+ var regex = new RegExp(route_regexp = '^' + this.formalizeQueryString(f_r) + '\\/$');
return regex;
},
+ formalizeQueryString: function(route) {
+ var r = route.replace(/\?/g, '\\?');
+ return r;
+ },
+
setSpecialItems: function(route) {
if(route.controller) {
this.named_items['controller'] = route.controller;

0 comments on commit 70ee989

Please sign in to comment.