Your words and code differ a bit, but this is not a problem. But the most
beautiful thing, you don't attempt to do anywhere what you say you want.
You say you want ServerController to inherit from Server and to do it, uyou
require it in the module. Then later on you instantiate the server and
attach env. That way it will never work. What you do in server
instantiation (adding that ENV) is not yet available at the time you start
the controller module.
The modules are executed at require time. But your actual objects are
instantiated later from those prototypes (which do not yet have ENV).
Why don't you simply 'init' both with ENV there? The name suggests you
won't be altering it. Or if you do want to share access to the same ENV the
way Java does, it's simpler to just have some getter and setter on the
server so that ctrl can reach for it, or the other way around. Or make a
better use of class/object/inheritance pattern so that you can use
something like super().ENV from controller. But it's not how it's usually
done in node.
To get more details, you should share more of the requirements - why do you
want to share these? Why do you need to separate them completely from one
another, but have them share the ENV? Do you need that ENV to also be
writable? By both server and ctrl?
On Monday, March 2, 2015 at 5:15:09 AM UTC+1, Aaron Martone wrote:
>
> Sorry in advance, I've been at this problem for 6 days now and have failed
> at every single turn on what I need to do. I simply cannot afford to waste
> more time, so I'm asking for help.
>
> My directory structure looks like this:
>
> /root
> ____/controllers
> ________server.ctrl.js
> ____/libs
> ________/classes
> ____________Server.js
> ____________ServerController.js
> server.app.js
>
> I am trying to create a "Server" class based on /libs/classes/Server.js.
> When instantiated, it takes 1 param, the constant ENV (environment), which
> is stored in its THIS scope.
> One of the properties of the "Server" object is called 'ctrl' and that
> should be set to an instance of the 'ServerController' class at
> /libs/classes/ServerController.js
>
> The 'ServerController' needs to inherit via the prototype chain, the
> properties on the 'Server' so that it can gain access to the 'THIS' scope's
> 'ENV' when it references 'this.ENV'.
> I have tried everything I can think of, many times 20x over, and cannot
> figure out how this is done. Hopefully I have explained my situation well
> enough. I'm able to do inheritance without modules, but something about
> Node's module exporting is throwing me off my understanding.
>
> *FYI*. The ServerController 'class' requires the /controllers/server.ctrl
> module which exports an object of functions that the ServerController class
> returns as direct functions off itself. Since ServerController gets stored
> in the Server object's 'ctrl' property, I was hoping I could call it via
> Server.ctrl.functionName();
>
>
> *==================================================================*
> *FROM HERE BELOW ARE THINGS I'VE TRIED, NOT NECESSARY TO THE QUESTION*
> *==================================================================*
>
>
>
> *in server.app.js:*
> var Server = require('./libs/classes/Server'); // the 'Server' class.
> var ServerController = require('./libs/classes/ServerController'); // the
> 'ServerController' class.
>
> var ctrl = new ServerController();
> var server = new Server(ENV, ctrl);
>
> *in Server.js*:
> function Server(ENV, ctrl) { this.ENV = ENV; this.ctrl = ctrl; }
> Server.prototype.test = function() { console.log('Server-level test'); }
> module.exports = Server;
>
> *in ServerController.js:*
> var Server = require('./Server');
> function ServerController() { }
> ServerController.prototype = Object.create(Server.prototype);
> ServerController.prototype.test = function() {
> console.log('Controller-level test, shadows Server-level'); }
> module.exports = ServerController;
>
> *Then I try to run things like:*
> server.test(); // Expecting 'Server-level test'
> server.ctrl.test(); // Expecting 'Controller-level test, shadows
> Server-level');
>
> But I'm running into tons of errors.
>
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/a3bd882a-24d4-4e2e-924f-bdbfb9d84b8e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.