Permalink
Browse files

Refine portal view.

  • Loading branch information...
1 parent 354ca34 commit 4de2cece66c8eef5dfa9d748a20100195d59ab58 @JacksonTian committed Dec 12, 2011
Showing with 136 additions and 117 deletions.
  1. +2 −2 models/post.js
  2. +1 −1 partials/comments.view
  3. +16 −108 portal.js
  4. +107 −0 portalview.js
  5. +10 −6 server.js
View
@@ -10,8 +10,8 @@ model.before = function (callback) {
callback(data);
}, 100);
};
-model.pipe = function (viewName, data, callback) {
+model.pipe = function (data, callback) {
setTimeout(function () {
- callback(viewName + "Pipe Content" + Math.random());
+ callback(data.viewName + "'s Pipe Content " + Math.random());
}, Math.random() * 1000);
};
View
@@ -1 +1 @@
-Comments
+Comments View
View
124 portal.js
@@ -1,11 +1,8 @@
-var ping = require("./ping");
var url = require("url");
var path = require("path");
var fs = require("fs");
var vm = require("vm");
-var util = require("util");
-var EventProxy = require("eventproxy").EventProxy;
-var Asset = require("./asset").Asset;
+var PortalView = require("./portalview").PortalView;
var Portal = function () {
};
@@ -14,102 +11,22 @@ Portal.templateSettings = {
evaluate : /<%([\s\S]+?)%>/g,
interpolate : /<%=([\s\S]+?)%>/g
};
-var PortalView = function () {
- EventProxy.call(this);
- this.ajaxSeq = [];
- this.partialViews = {};
- this.partialSeq = [];
- this.pipeSeq = [];
- this.pipes = [];
- var that = this;
- this.on("partial", function (data) {
- var viewPath = path.join("partials", data.viewName + ".view");
- fs.readFile(viewPath, function (err, view) {
- if (err) {
- throw err;
- } else {
- that.partialViews[data.viewName] = view.toString("utf-8");
- that.fire("partial_end", view.toString("utf-8"));
- }
- });
- });
-
- this.on("pipe", function (data) {
- that.model.pipe(data.viewName, data.viewData, function (content) {
- var script = '<script>\nPortal.bigPipe("' + data.viewName + '", "' + content + '");\n</script>';
- that.pipes.push(script);
- that.fire("pipe_end", script);
- });
- });
-};
-util.inherits(PortalView, EventProxy);
-PortalView.prototype.partial = function (viewName, data) {
- console.log(arguments);
- this.partialSeq.push({"viewName": viewName, "viewData": data});
- return "<%=" + viewName + "%>";
-};
-
-PortalView.prototype.ajax = function (viewName, data) {
- var script = '<script>\nPortal.fire("' + viewName + '", ' + JSON.stringify(data) + ');\n</script>';
- this.ajaxSeq.push(script);
- return "";
-};
-
-PortalView.prototype.bigPipe = function (viewName, data) {
- data = "BigPipe rendered."
- this.pipeSeq.push({"viewName": viewName, "viewData": data});
- return "";
-};
-PortalView.prototype.ignitePartials = function () {
- var that = this;
- this.partialSeq.forEach(function (val) {
- that.fire("partial", val);
- });
-};
-PortalView.prototype.ignitePipes = function () {
- var that = this;
- this.pipeSeq.forEach(function (val) {
- that.fire("pipe", val);
- });
-};
-PortalView.prototype.processAjax = function (response) {
- this.ajaxSeq.forEach(function (val) {
- response.write(val);
- });
-};
-PortalView.prototype.processPipes = function (response) {
- // Process sequnences
- var times = this.pipeSeq.length - this.pipes.length;
- console.log(times);
- this.pipes.forEach(function (script) {
- response.write(script);
- });
- // Process coming pipes.
- this.on("pipe_end", function (script) {
- response.write(script);
- });
- this.after("pipe_end", times, function () {
- console.log("Complete.");
- response.end();
- });
-};
-
-Portal.preParse = function (str, sandbox, settings) {
+Portal.preprocess = function (str, sandbox, settings) {
var c = settings || Portal.templateSettings;
var temp = str.replace(c.interpolate, function (match, code) {
return sandbox.viewData[code] ? sandbox.viewData[code] : "The key " + code + " is undefined.";
}).replace(c.evaluate, function (match, code) {
- console.log(code);
+ //console.log(code);
var result = vm.runInNewContext(code, sandbox);
- console.log(result);
+ //console.log(result);
return result;
});
return temp;
};
-Portal.postParse = function (str, sandbox, settings) {
+Portal.postprocess = function (str, sandbox, settings) {
var c = settings || Portal.templateSettings;
var temp = str.replace(c.interpolate, function (match, code) {
return sandbox[code] ? sandbox[code] : "The key " + code + " is undefined.";
@@ -132,31 +49,30 @@ Portal.prototype.dispatch = function (request, response) {
console.log(realPath);
var portalView = new PortalView();
- console.log(portalView);
portalView.all("before", "file", function (viewData, file) {
- console.log("Ready!");
+ console.log("Portal ready.");
var sandbox = {
"view": portalView,
"viewData": viewData
};
- var preParsed = Portal.preParse(file, sandbox);
- console.log("Pre processed.");
+
+ var preprocessed = Portal.preprocess(file, sandbox);
portalView.after("partial_end", portalView.partialSeq.length, function () {
response.writeHead(200);
- response.write(Portal.postParse(preParsed, portalView.partialViews));
- portalView.fire("render_phase1_done");
+ response.write(Portal.postprocess(preprocessed, portalView.partialViews));
+ console.log("Postprocess done.");
+ portalView.fire("postprocess_done");
});
- portalView.ignitePartials();
- portalView.ignitePipes();
+ portalView.getPartials();
+ portalView.getPipes();
- console.log("Ignited all async view.");
+ console.log("Preprocess done.");
});
- portalView.on("render_phase1_done", function () {
- console.log("Render_phase1_done");
+ portalView.on("postprocess_done", function () {
portalView.processAjax(response);
portalView.processPipes(response);
});
@@ -185,12 +101,4 @@ Portal.prototype.dispatch = function (request, response) {
};
-var portal = new Portal();
-ping.createServer(portal).listen(8000);
-console.log("Portal server is running at 8000 port.");
-
-// Static file server.
-var asset = new Asset();
-ping.createServer(asset).listen(8001);
-console.log("Static file server is running at 8001 port.");
-
+exports.Portal = Portal;
View
@@ -0,0 +1,107 @@
+var util = require("util");
+var path = require("path");
+var fs = require("fs");
+var EventProxy = require("eventproxy").EventProxy;
+
+var PortalView = function () {
+ EventProxy.call(this);
+ this.ajaxSeq = [];
+ this.partialViews = {};
+ this.partialSeq = [];
+ this.pipeSeq = [];
+ this.pipes = [];
+};
+util.inherits(PortalView, EventProxy);
+
+/**
+ * Put partial view into sequence. Replace call with view name in template.
+ * After preprocess phase, portal will get all partial views through this sequence.
+ * The view name will be replaced in postprocess phase.
+ */
+PortalView.prototype.partial = function (viewName, data) {
+ this.partialSeq.push({"viewName": viewName, "viewData": data});
+ return "<%=" + viewName + "%>";
+};
+
+/**
+ * Put ajax call into sequence. These scripts will were outputed after all partial views outputed.
+ */
+PortalView.prototype.ajax = function (viewName, data) {
+ var script = '<script>\nPortal.fire("' + viewName + '", ' + JSON.stringify(data) + ');\n</script>';
+ this.ajaxSeq.push(script);
+ return "";
+};
+
+/**
+ * Put pipe call into sequence. Trigger these calls after preprocess phase.
+ */
+PortalView.prototype.bigPipe = function (viewName, data) {
+ this.pipeSeq.push({"viewName": viewName, "viewData": data});
+ return "";
+};
+
+/**
+ *
+ */
+PortalView.prototype.getPartial = function (data) {
+ var that = this;
+ var viewPath = path.join("partials", data.viewName + ".view");
+ fs.readFile(viewPath, function (err, view) {
+ if (err) {
+ throw err;
+ } else {
+ that.partialViews[data.viewName] = view.toString("utf-8");
+ that.fire("partial_end", view.toString("utf-8"));
+ }
+ });
+};
+
+PortalView.prototype.getPartials = function () {
+ var that = this;
+ this.partialSeq.forEach(function (val) {
+ that.getPartial(val);
+ });
+};
+
+
+PortalView.prototype.getPipe = function (data) {
+ var that = this
+ this.model.pipe(data, function (content) {
+ var script = '<script>\nPortal.bigPipe("' + data.viewName + '", "' + content + '");\n</script>';
+ that.pipes.push(script);
+ that.fire("pipe_end", script);
+ });
+};
+
+PortalView.prototype.getPipes = function () {
+ var that = this;
+ this.pipeSeq.forEach(function (val) {
+ that.getPipe(val);
+ });
+};
+
+PortalView.prototype.processAjax = function (response) {
+ this.ajaxSeq.forEach(function (script) {
+ response.write(script);
+ });
+};
+
+PortalView.prototype.processPipes = function (response) {
+ // Process sequnences
+ this.pipes.forEach(function (script) {
+ response.write(script);
+ });
+
+ // Process coming pipes.
+ var times = this.pipeSeq.length - this.pipes.length;
+ this.on("pipe_end", function (script) {
+ response.write(script);
+ });
+
+ this.after("pipe_end", times, function () {
+ console.log("Complete.");
+ response.end();
+ });
+};
+
+exports.PortalView = PortalView;
View
@@ -2,14 +2,18 @@ var http = require("http");
var ping = require("./ping");
var Framework = require("./framework").Framework;
var Asset = require("./asset").Asset;
+var Portal = require("./portal").Portal;
-// Dynamic handle.
-var framework = new Framework();
-ping.createServer(framework).listen(8000);
-console.log("Running at 8000 port.");
+var portal = new Portal();
+ping.createServer(portal).listen(8000);
+console.log("Portal server is running at 8000 port.");
// Static file server.
var asset = new Asset();
-ping.createServer(asset).listen(8080);
+ping.createServer(asset).listen(8001);
+console.log("Static file server is running at 8001 port.");
-console.log("Running at 8080 port.");
+// Dynamic handle.
+var framework = new Framework();
+ping.createServer(framework).listen(8002);
+console.log("Dynamic server is running at 8002 port.");

0 comments on commit 4de2cec

Please sign in to comment.