Controller scope

31 views
Skip to first unread message

Jef Harkay

unread,
Apr 11, 2014, 2:37:22 PM4/11/14
to
Let's say I have a controller, and within an action, I want to access a scope variable, but whenever I'm inside of the action, this is an empty object.  How do I make the this refer to the instantiated class of my controller?  Here's some code for visual purposes:

var Application = require('../application');

var MyController = module.exports = function MyController(init) {
 
Application.call(this, init);
 
this.blah = 2;  // I obviously was desperate and trying multiple options...
};
this.blah = 1;  // I obviously was desperate and trying multiple options...

// This is a route defined in my routes file
MyController.prototype.test = function test(controller) {
  console
.log(this);  // empty object
  console
.log(this.blah);  // undefined, but I want to access this value... and no, I don't want to do var blah = 1;
 
return controller.send({success: true});
};

I even tried adding .bind(this) to the end of the test function, but no dice.  Any thoughts on how I can accomplish this?

Jef Harkay

unread,
Apr 25, 2014, 12:30:14 PM4/25/14
to compo...@googlegroups.com
This really is an unfortunate problem... what I'm ultimately trying to do is create a logging mechanism, and I want each controller to use it, but right now, I have to define a global log variable, so I can instantiate it in my controller's constructor, and then I can use it in my actions, but I have to do this for each an every controller, which is rather annoying.  I just want the controllers to have them on their scope, and have one point of instantiation for them all.  It's more like a mixin, I guess.

I have tried messing with the global object by creating my log in server.js and appending it to the global object, but I don't really like this solution.  It works for now, but I'd rather have something be mixed in, and then be able to access it either through this or controller that's passed in.

Ondreian

unread,
Apr 26, 2014, 11:48:28 AM4/26/14
to
You should use controllerExtensions during the app initialization process.


Caching = require 'caching'
module.exports = (compound)->
  compound
.controllerExtensions.cache = new Caching 'redis'



This is an example that might go in config/initializers/

You could use it in any controller action
 by using c.cache as the "c" object is the current context the route is being evaluated with.

Message has been deleted

Jef Harkay

unread,
Apr 28, 2014, 9:50:31 AM4/28/14
to compo...@googlegroups.com
That's a very interesting piece of information to know.  Thanks!

Ondreian

unread,
May 13, 2014, 10:55:27 AM5/13/14
to compo...@googlegroups.com
No problem Jef,

There are quite a few neat undocumented features to compound.js, and i've found reading the source code to be a great resource for learning more about the inner workings of it.

Ironically, this past week I was working on some stuff in a Compound app, and I felt I was repeating myself way more often than I should be.  

It seemed that configuration on a controller level, and not an application level (ala compound.controllerExtensions) was needed so I put together a little module to give Controller more contextual juice.

It's published on NPM  `npm install co.ntextualize` && add it to your autoloaders

Or peruse the code (and examples) here:


Pull requests & bug reports are always appreciated





Jef Harkay

unread,
May 23, 2014, 4:21:43 PM5/23/14
to compo...@googlegroups.com
Interesting.  I will definitely check that out when I get a moment... sounds like something I'd find useful.  Thanks for sharing!
Reply all
Reply to author
Forward
0 new messages