Permalink
Browse files

template view processing, with test

  • Loading branch information...
1 parent 5556a5b commit e8bc7aceb8fd441da024041fcb06a98a04693a64 @ajlopez committed May 26, 2012
Showing with 56 additions and 3 deletions.
  1. +20 −3 lib/simplemvc.js
  2. +36 −0 test/processTemplateView.js
View
@@ -1,5 +1,6 @@
-var fs = require('fs');
+var fs = require('fs'),
+ template = require('./template.js');
function isFile(filename)
{
@@ -48,10 +49,26 @@ function Application()
if (isFile(viewfn))
viewfn = readFile(viewfn);
- views[name] = function(req, res) { res.write(viewfn); res.end(); };
+ if (template.isTemplateText(viewfn))
+ {
+ var tpl = template.compileTemplate(viewfn);
+ views[name] = function(req, res, model, context) {
+ tpl(res, model,context);
+ res.end();
+ };
+ return;
+ }
+ else
+ {
+ views[name] = function(req, res) { res.write(viewfn); res.end(); };
+ return;
+ }
}
else
+ {
views[name] = viewfn;
+ return;
+ }
}
this.registerActions = function(app)
@@ -91,7 +108,7 @@ function Application()
function processView(req, res, viewname, model, context)
{
- views[viewname](req, res, model, context);
+ views[viewname](req, res, model, context != null ? context : {});
res.end();
}
}
@@ -0,0 +1,36 @@
+
+var assert = require('assert'),
+ simplemvc = require('../');
+
+var application = simplemvc.createApplication();
+
+// Register action by view name
+
+application.get('/', 'home');
+
+// Register view
+
+application.view('home', '1 + 2 == ${1+2}');
+
+var app = {
+ gets: {},
+ get: function(url, fn)
+ {
+ this.gets[url] = fn;
+ }
+};
+
+application.registerActions(app);
+
+var req = {};
+var res = {
+ output: '',
+ write: function(text) { this.output += text;},
+ end: function() {}
+};
+
+app.gets['/'](req, res);
+
+assert.ok(res.output);
+assert.equal(res.output, "1 + 2 == 3");
+

0 comments on commit e8bc7ac

Please sign in to comment.