Last active
December 10, 2015 20:38
-
-
Save hayes/4489830 to your computer and use it in GitHub Desktop.
basic controller inheritance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Controller = require('base_controller'); | |
var Application = module.exports = Controller.extend(function Application(init) { | |
init.before(function protectFromForgeryHook(ctl) { | |
ctl.protectFromForgery('13cf581adb4cd3c1cccce3604a6c044215f21679'); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}}); | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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