Permalink
Browse files

Can now specify which view to render. Show error page if template ren…

…dering errors.
  • Loading branch information...
1 parent 32f58a3 commit 5b5dc2286f8299e8563d8e53dbba0e40030625d3 @thatismatt committed Jun 12, 2010
@@ -2,5 +2,20 @@ var actionresults = require('josi/actionresults');
var redirect = actionresults.redirect;
var view = actionresults.view;
-this.index = function() { return view({ title: 'A Title', name: 'Kate' }); };
-this.redir = function() { return redirect('/product/details/1'); }
+this.index = function() {
+ return view({
+ title: 'A Title',
+ details: 'Some details'
+ });
+};
+
+this.another = function() {
+ return view({
+ title: 'Another Title',
+ details: 'A different action, using the same view.'
+ }, 'index');
+};
+
+this.redir = function() {
+ return redirect('/product/details/1');
+};
@@ -1 +1 @@
-<p>I Love <%= name %></p>
+<p>Details: <%= details %></p>
@@ -5,9 +5,10 @@
<link rel="stylesheet" href="/static/style.css" type="text/css" charset="utf-8"></link>
</head>
<body>
- <h1><%= title %></h1>
+ <h1><a href="/"><%= title %></a></h1>
<ul class="nav">
<li><a href="/home/redir">A redirect</a></li>
+ <li><a href="/home/another">An action sharing a view</a></li>
<li><a href="/product">List of products</a></li>
<li><a href="/error">Throw an error</a></li>
</ul>
@@ -19,18 +19,24 @@ this.ActionResult = ActionResult = Class.extend({
});
this.ViewResult = ViewResult = ActionResult.extend({
- init: function(data, view, master) {
+ init: function(data, action, controller) {
this._super();
this.data = data;
- this.view = view;
- this.master = master;
+ this.context = {
+ controller: controller,
+ action: action
+ };
},
execute: function(req, res, app) {
var viewResult = this;
var execute = this._super;
app.templater.render(
this.data,
- function(rendered) {
+ function(err, rendered) {
+ if (err) {
+ error(err).execute(req, res);
+ return;
+ }
viewResult.body = rendered;
execute.apply(viewResult, [req, res]);
},
@@ -104,8 +110,8 @@ this.async = function(func) {
return new AsyncResult(func);
};
-this.view = function(data) {
- return new ViewResult(data);
+this.view = function(data, action, controller) {
+ return new ViewResult(data, action, controller);
};
this.redirect = function(url) {
View
@@ -22,7 +22,8 @@ this.Server = function(dir) {
var processActionResult = function(actionResult, context) {
if (actionResult instanceof actionresults.ActionResult) {
if (actionResult instanceof actionresults.ViewResult) {
- actionResult.view = 'views/' + context.route.controller + '/' + context.route.action + '.html';
+ actionResult.view = 'views/' + (actionResult.context.controller || context.route.controller) +
+ '/' + (actionResult.context.action || context.route.action) + '.html';
actionResult.master = 'views/master.html';
}
return actionResult;
@@ -26,16 +26,20 @@ var TemplatingEngine = Class.extend({
render: function(data, callback, view, master) {
var self = this;
this.load(view, function(template) {
- var rendered = template(data);
+ try {
+ var rendered = template(data);
+ } catch (e) {
+ callback(e);
+ }
if (master) {
// fixme: don't clobber users' data
data.main = rendered;
self.load(master, function(masterTemplate) {
var rendered = masterTemplate(data);
- callback(rendered);
+ callback(null, rendered);
});
} else {
- callback(rendered);
+ callback(null, rendered);
}
});
}

0 comments on commit 5b5dc22

Please sign in to comment.