-
Notifications
You must be signed in to change notification settings - Fork 11
inter controller communication
shimondoodkin edited this page Dec 4, 2010
·
7 revisions
Inter-controller communication
This is part 2 of example for implementing philosophy for a framework
I start over with something similar and simple.
I add to the app object the flowing objects: controllers , an async library,controller1 and controller2.
controller1.js
this.main=function(app)
{
// code here
var controller={};
controller.main=function()
{
shared.res.writeHead(200,{'Content-Type':'text/html'})
shared.res.end(app.controllers.controller2.getfoo());
}
return controller;
}
controller2.js
this.main=function(app)
{
// code here
var controller={};
controller.getfoo=function(shared)
{
retuurn "foo";
}
return controller;
}
var url = require('url');
var http = require('http');
var app={};
app.fs=require('fs'); // shared loaded modules
app.inflow=require('inflow'); // some async library
http.createServer(function (req, res) {
app.controllers={};
app.controllers.controller1 = require('controller1.js').main(app);
app.controllers.controller2 = require('controller2.js').main(app);
var shared={ req: req, res:res, parsedurl:url, qs:url.querystring };
app.controllers.controller1.main(shared);
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
async also works:
controller1.js
this.main=function(app)
{
// code here
var controller={};
controller.render=function()
{
this.shared.res.writeHead(200,{'Content-Type':'text/html'})
this.shared.res.end(this.shared.foo_value);
}
controller.main=function(shared)
{
app.inflow(shared,[ app.controllers.controller2.get_foo, controller.render ])
// using a function from other controller like this is power.
}
return controller;
}
controller2.js
this.main=function(app)
{
// code here
var controller={};
controller.getfoo=function(shared)
{
this.shared.foo_value="foo";
this.next();
}
return controller;
}
var url = require('url');
var http = require('http');
var app={};
app.fs=require('fs'); // shared loaded modules
app.inflow=require('inflow'); // some async library
http.createServer(function (req, res) {
app.controllers={};
app.controllers.controller1 = require('controller1.js').main(app);
app.controllers.controller2 = require('controller2.js').main(app);
var shared={ req: req, res:res, parsedurl:url, qs:url.querystring };
app.controllers.controller1.main(shared);
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
other ways to create controllers
This document is in process. Everyone is encouraged to participate.
Additions and Comments are welcome in this page:
Comments:
also you can post an issue at https://github.com/shimondoodkin/nodejs-mongodb-app/issues
Philosophy for a framework, with practical examples: