jaredhanson / locomotive

Powerful MVC web framework for Node.js.
http://locomotivejs.org/
MIT License
888 stars 135 forks source link

Allow for "global" filters #123

Open RaphiePS opened 10 years ago

RaphiePS commented 10 years ago

There should be a "nice" way to have something run before all actions. For example, say I'm using connect-flash. I'd want to pass flash messages into every page, so it gets rather repetitive to have this in every controller:

controller.before('*', function(next) {
  this.messages = this.req.flash('messages');
  this.errors = this.req.flash('errors');
  // etc
  next();
});

I suppose something like this could be put in the middleware initializer, but in my mind initializers should represent application setup, not logic.

If you agree, I'd be happy to submit a pull request!

jaredhanson commented 10 years ago

It should be possible to do this using controller inheritance, which is viable now in 0.4, but hasn't been documented yet.

If you have another proposal for solving this, I'd be happy to consider it!

Sent from my iPhone

On Dec 17, 2013, at 10:52 PM, Raphie Palefsky-Smith notifications@github.com wrote:

There should be a "nice" way to have something run before all actions. For example, say I'm using connect-flash. I'd want to pass flash messages into every page, so it gets rather repetitive to have this in every controller:

controller.before('*', function(next) { this.messages = this.req.flash('messages'); this.errors = this.req.flash('errors'); // etc next(); }); I suppose something like this could be put in the middleware initializer, but in my mind initializers should represent application setup, not logic.

If you agree, I'd be happy to submit a pull request!

— Reply to this email directly or view it on GitHub.

RaphiePS commented 10 years ago

Didn't know that existed! Inheritance sounds perfect. I just took a glance at controller.js and couldn't find anything related to inheritance, but perhaps I didn't look hard enough.

jaredhanson commented 10 years ago

Take a look at instantiators/constructor.js (I think)

Sent from my iPhone

On Dec 17, 2013, at 11:17 PM, Raphie Palefsky-Smith notifications@github.com wrote:

Didn't know that existed! Inheritance sounds perfect. I just took a glance at controller.js and couldn't find anything related to inheritance, but perhaps I didn't look hard enough.

— Reply to this email directly or view it on GitHub.

redking commented 10 years ago

Do you plan on releasing a changelog or updated docs for the latest release? I wasn't aware that controller inheritance had been added!

jaredhanson commented 10 years ago

Eventually, yes. But no definitive timeline.

Sent from my iPhone

On Dec 18, 2013, at 1:24 AM, Cormac Flynn notifications@github.com wrote:

Do you plan on releasing a changelog or updated docs for the latest release? I wasn't aware that controller inheritance had been added!

— Reply to this email directly or view it on GitHub.