Skip to content

Commit

Permalink
Added request and router methods to BaseController - no more hardcode…
Browse files Browse the repository at this point in the history
…d attributes.
  • Loading branch information
fk1blow committed Jun 4, 2011
1 parent 5ab381e commit a6d0682
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions lib/Controller/BaseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ var ActiveRouter = require('../Router/ActiveRouter'),
Request = require('../Http/Request');


function toUpcase(string) {
return string.substr(0, 1).toUpperCase() + string.substr(1);
}


var BaseController = Klass({
attributes: {
controller_name: function() { return ActiveRouter.getControllerName() },

action_name: function() { return ActiveRouter.getActionName() },

/**
* I can't tell if it's safe to expose all the Request object
*/
request: function() { return Request.Factory() }
},
attributes: {},

methods: {
render: function(view, options) {
Expand All @@ -22,6 +18,30 @@ var BaseController = Klass({

renderToResponse: function(string_output, content_type) {
View.renderToResponse(string_output, content_type);
},

request: function(field) {
if(field && field.length > 0) {
var f_method = 'get' + toUpcase(field);
try {
return Request.Factory()[f_method]();
} catch(e) {
throw new Error('Unable to call ' + f_method);
}
}
return null;
},

router: function(field) {
if(field && field.length > 0) {
var f_method = 'get' + toUpcase(field);
try {
return ActiveRouter[f_method]();
} catch(e) {
throw new Error('Unable to call ' + f_method);
}
}
return null;
}
}
});
Expand Down

0 comments on commit a6d0682

Please sign in to comment.