-
Notifications
You must be signed in to change notification settings - Fork 11
controller
shimondoodkin edited this page Nov 20, 2010
·
1 revision
a basic example to begin with:
this.page=function(app,model)
{
var page=
{
pagethis:this,
pagearguments:arguments,
pagefilename:__filename,
pageload:(new Date),
pageurl:'changethis',
load_templates:
{
// templates_view:"website/pijimi_heb/views/invite/templates.html"
},
prepeare_templates:{},
prepere_data:function (page,template_name,callback){callback({'page':page, 'app':app, 'req':{}, });},
//your functions here
main:function (shared,i)
{
var self = this; var res = shared.res;var req = shared.req , qry=req.parsedurl.query;
//your code here, for example:
res.writeHead(200, {'Content-Type':'text/html; charset=utf-8'});
res.write('hello world');
res.end();
/// end your code here
return true;
}
}
return page;
} ;
full example:
this.page=function(app,model)
{
var page=
{
pagethis:this, // used to reload only this controller
pagearguments:arguments, // used to reload only this controller
pagefilename:__filename, // used to reload only this controller
pageload:(new Date), // used to reload only this controller
pageurl:'ajax', // a url on the server it is prefixed with '/'
urlmatch:'path', //can be: 'path' or 'pathbegins'
load_templates:
{
templates_view:"website/pijimi_heb/views/invite/templates.html", // the element name will be the function name of template in page object.
},
prepeare_templates:{},
prepere_data:function (page,template_name,callback){callback({'page':page, 'app':app, 'req':{}, });}, // simple prepare data function
render:function(){ // some function which is inside this controller. you can add more functions like this.
var self = this; var shared = this.shared;var res = shared.res; // shortcuts to what you want to use
res.writeHead(200, shared.header);
res.write(shared.content);
res.end();
},
load_page_info :function (){
app.models.mypages.getall( { name: this.shared.page_name }, this.next ); // getall is mongodb.find({}) , cursor.toArray
},
main:function (shared,i) // shared object you receive from handle http request, i is the page position in routes array (you can route i+1 to continue)
{
var self = this; var res = shared.res;var req = shared.req , qry=req.parsedurl.query; // shortcuts to what you want to use
var steps = [];
var index = app.pages.index; // ref to 'index' named page controller
var common = app.pages.common; // ref to 'common' named page controller
switch (qry.action)
{
case 'checkmail':
shared.content = 'true';
steps = [index.checkmail , page.render]
break;
case 'register':
shared.content = 'false';
steps = [index.checkmail , index.checkmobile , index.register , page.render];
break;
case 'changepass':
shared.content = 'false';
steps = [common.requireuser , index.checkpass , index.login , index.changepass , page.render];
break;
case 'pageinfo':
shared.view=page.templates_view;
shared.page_name=qry.page; // mywebsite.com/ajax?action=pageinfo&page=...
steps = [page.load_page_info, common.render]; // common.render is calls a template that is shared.view and does res.write...
break;
}
app.inflow.flow(shared,steps);
return true; // if it is a good route match then return true, otherwise return false.
// usually at the beginning of the page you return false if the URL isn't matching
}
}
return page;
} ;