Skip to content

Instantly share code, notes, and snippets.

@hayes
Last active December 10, 2015 20:38
Show Gist options
  • Save hayes/4489830 to your computer and use it in GitHub Desktop.
Save hayes/4489830 to your computer and use it in GitHub Desktop.
basic controller inheritance
var Controller = require('base_controller');
var Application = module.exports = Controller.extend(function Application(init) {
init.before(function protectFromForgeryHook(ctl) {
ctl.protectFromForgery('13cf581adb4cd3c1cccce3604a6c044215f21679');
});
});
module.exports = function Controller () {};
Controller.extend = function(initializer) {
var self = this;
initializer = initializer || function(){};
var controller = function(init) {
self.call(this, init);
return initializer.call(this, init);
}
Object.keys(this).forEach(function(key) {
controller[key] = self[key];
});
controller.prototype = Object.create(this.prototype);
return controller;
}
Controller.action = function(name, fn) {
this.prototype[name] = fn;
}
var Application = require('./application');
var UserController = module.exports = Application.extend(function (init) {
init.before(loadUser, {
only: ['show', 'edit', 'update', 'destroy']
});
});
UserController.action('new', function (c) {
this.title = 'New user';
this.user = new (c.User);
c.render();
});
//I don't really like prefixing everything with c. I would probably mod my controllers to look like:
UserController.action('new', function (c) { with(c) {
this.title = 'New user';
this.user = new (User);
render();
}});
...
//for comparison
load('application');
before(loadUser, {
only: ['show', 'edit', 'update', 'destroy']
});
action('new', function () {
this.title = 'New user';
this.user = new User;
render();
});
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment