express-resource-new icon indicating copy to clipboard operation
express-resource-new copied to clipboard

Bind the controller to each resource-callback to allow passing a class i...

Open ctavan opened this issue 12 years ago • 1 comments

...nstance as a controller.

This patch allows passing a class-instance as a controller. Consider the following pseudocode as an example:

Controller.js:

function Controller(Model) {
  this.Model = Model;
}

Controller.prototype.index = function(req, res, next) {
  this.Model.find(function(err, entries) {
    if (err) return next(err);

    res.send(entries);
  }
}

then in controllers/users/index.js:

var Controller = require('../Controller.js');

module.exports = (function() { return new Controller(UserModel) })();

Personally I have my own controller initialization logic and I overwrite the app._load() method to inject my controller instances.

Let me know what you think!

ctavan avatar Jun 17 '13 11:06 ctavan

:+1:

ekryski avatar Sep 02 '14 21:09 ekryski