Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Renamed the main controller; removed the actionrouter - actually move…

…d it to the router where it belongs; moved the standardizr inside the main controller file
  • Loading branch information...
commit 8486ff441776faca9353e585642a9fa5b7826771 1 parent 3aba336
@fk1blow authored
View
107 lib/Controller/ActionController.js
@@ -1,107 +0,0 @@
-var Config = require('../Config'),
- File = require('../File/File'),
- Standardizr = require('./Standardizr'),
- ActionRouter = require('./ActionRouter'),
- BaseController = require('./BaseController'),
- Validator = require('./Validator');
-
-
-var ActionDispatcher = Klass({
- statics: {
- dispatch: function(res) {
- Controller.init();
- if(Config.env.default_response)
- res.end(Config.env.default_response);
- }
- }
-});
-
-
-var Controller = Klass({
- statics: {
- controller: {
- default_name: null,
- object_name: null,
- instance: null,
- action: null,
- },
-
- init: function() {
- this._setDefaults();
-
- // Validate
- this._validateNames();
-
- // Here be dragons
- this._loadAppController();
-
- // Builds the instance
- this._buildInstance();
-
- // Invokes the action
- this._callAction();
- },
-
- _validateNames: function() {
- var cn_valid = Validator.validate('c_n', this.controller.default_name);
- var co_valid = Validator.validate('c_o', this.controller.object_name);
- var an_valid = Validator.validate('a_n', this.controller.action);
-
- if(!cn_valid) {
- throw new Error('Invalid controller route name:: ' + this.controller.default_name);
- }
- if(!co_valid) {
- throw new Error('Invalid controller object name:: ' + this.controller.object_name);
- }
- if(!an_valid) {
- throw new Error('Invalid action name:: ' + this.controller.action);
- }
- },
-
- _setDefaults: function() {
- this.controller.default_name = ActionRouter.getControllerName();
- this.controller.object_name = Standardizr.standardizeName(this.controller.default_name);
- this.controller.action = ActionRouter.getActionName();
- },
-
- // The magic instance builder
- _buildInstance: function() {
- var c = this._loadControllerFile(this.controller.object_name);
-
- // Constructing instance
- this.controller.instance = new c();
- },
-
- // Invokes controller's action
- _callAction: function() {
- var action = Standardizr.getControllerAction();
-
- if(typeof(this.controller.instance[action]) != 'function') {
- throw new Error('Unable to find action:: ' + action);
- }
-
- // Calling the controller's instance method (action)
- this.controller.instance[action]();
- },
-
- // Loads the ApplicationController
- _loadAppController: function() {
- if(typeof(ApplicationController) != 'function') {
- var f = this._loadControllerFile('ApplicationController');
- }
- if(typeof(f) != 'function') {
- throw new Error('Unable to find ApplicationController');
- }
- },
-
- // Loads a controller givend the object name EX: 'UsersController'
- _loadControllerFile: function(controller_object_name) {
- var file = controller_object_name;
- var f = File.loadFile(Config.getPathFor('controllers') + '/' + file);
- return f;
- }
- }
-});
-
-
-module.exports = ActionDispatcher;
View
47 lib/Controller/ActionRouter.js
@@ -1,47 +0,0 @@
-var ActiveRouter = require('../Router/ActiveRouter'),
- Request = require('../Http/Request');
-
-
-var ActionRouter = Klass({
- statics: {
- route: null,
-
- getControllerName: function() {
- var route_controller = this.getCurrentRoute().controller;
- // This pseudo error handling must go
- if(!route_controller) {
- throw new Error('Unable to find a route for:: ' + Request.getUrl())
- }
- return route_controller;
- },
-
- getActionName: function() {
- var route_action = this.getCurrentRoute().action;
- if(!route_action) {
- throw new Error('Unable to find a route(action) for:: ' + Seek.URL.Request.getUrl())
- }
- return route_action;
- },
-
- getParams: function() {
- var route = this.getCurrentRoute();
- var params = {};
- for(var item in route) {
- if(item != 'controller' && item != 'action')
- params[item] = route[item];
- }
- return params || null;
- },
-
- getUrl: function() {
- return this.getCurrentRoute().current_url;
- },
-
- getCurrentRoute: function() {
- return ActiveRouter.getRoute();
- }
- }
-});
-
-
-module.exports = ActionRouter;
View
42 lib/Controller/Standardizr.js
@@ -1,42 +0,0 @@
-var ActionRouter = require('./ActionRouter');
-
-
-/**
- * @package ActionModule
- * @name Standardize
- */
-var Standardizr = Klass({
- statics: {
- prefix: 'Controller',
-
- object_name: null,
-
- standardizeName: function(controller_name) {
- this.object_name = this.toUpcase(controller_name)
- return this.object_name + this.prefix;
- },
-
- getControllerAction: function() {
- var r = ActionRouter.getActionName();
- return r || null;
- },
-
- toUpcase: function(string) {
- if(!this.isDefined(string)) return false;
- return string.substr(0, 1).toUpperCase() + string.substr(1);
- },
-
- isString: function(string) {
- if(!this.isDefined(string)) return false;
- var regexp = /^[a-zA-Z]+[a-zA-Z\_]+[a-zA-Z]+$/i;
- return regexp.test(string);
- },
-
- isDefined: function(string) {
- if(typeof(string) != 'undefined') return true;
- }
- }
-});
-
-
-module.exports = Standardizr;
Please sign in to comment.
Something went wrong with that request. Please try again.