Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Enhancement.

  • Loading branch information...
commit b308f1c64b75292e93720d2ac346169aff318c61 1 parent cadfdee
@JacksonTian authored
View
63 framework.js
@@ -43,55 +43,62 @@ Framework.prototype.dispatch = function (request, response) {
}
};
+
// Add get parse supports
-Framework.prototype.enableGet = function (request, response) {
- var _urlMap;
- request.get = function (key) {
- if (!_urlMap) {
- _urlMap = url.parse(request.url, true);
+Framework.prototype.enableGet = function () {
+ http.IncomingMessage.prototype.get = function (key) {
+ if (!this._urlMap) {
+ this._urlMap = url.parse(this.url, true);
}
- return _urlMap.query[key];
+ return this._urlMap.query[key];
};
};
// Add cookie parse and set supports
Framework.prototype.enableCookie = function (request, response) {
- var _cookieMap;
- request.cookie = function (key) {
- if (!_cookieMap) {
- _cookieMap = cookie.parse(request.headers.cookie || "");
- }
- return _cookieMap[key];
+ http.IncomingMessage.prototype.cookie = function () {
+ this.cookie = function (key) {
+ if (!this._cookieMap) {
+ this._cookieMap = cookie.parse(this.headers.cookie || "");
+ }
+ return this._cookieMap[key];
+ };
};
- var _setCookieMap = {};
- response.setCookie = function (cookieObj) {
- _setCookieMap[cookieObj.key] = cookie.stringify(cookieObj);
+
+ http.ServerResponse.prototype.setCookie = function (cookieObj) {
+ if (!this._setCookieMap) {
+ this._setCookieMap = {};
+ }
+ this._setCookieMap[cookieObj.key] = cookie.stringify(cookieObj);
var returnVal = [];
- for(var key in _setCookieMap) {
- returnVal.push(_setCookieMap[key]);
+ for(var key in this._setCookieMap) {
+ returnVal.push(this._setCookieMap[key]);
}
- response.setHeader("Set-Cookie", returnVal.join(", "));
+ this.setHeader("Set-Cookie", returnVal.join(", "));
};
};
// Add post parse supports
-Framework.prototype.enablePost = function (request, response) {
+Framework.prototype.enablePost = function () {
+ http.IncomingMessage.prototype.post = function () {
+ if (!this._postMap) {
+ this._postMap = qs.parse(this.postData);
+ }
+ return this._postMap[key];
+ };
+};
+
+// Recept post body.
+Framework.prototype.recept = function (request) {
if (request.method === "POST") {
- var _postData = "",
- _postMap = "";
+ var _postData = "";
- request.on('data', function (chunk) {
+ this.on('data', function (chunk) {
_postData += chunk;
})
.on("end", function () {
request.postData = _postData;
- request.post = function (key) {
- if (!_postMap) {
- _postMap = qs.parse(_postData);
- }
- return _postMap[key];
- };
});
}
};
View
4 models/post.js
@@ -5,7 +5,9 @@ model.before = function (callback) {
"title": "用Nodejs打造你的静态文件服务器",
"content": '<p>在《The Node Beginner Book》的中文版(<a href="http://nodebeginner.org/index-zh-cn.html">http://nodebeginner.org/index-zh-cn.html</a>)发布之后,获得国内的好评。也有同学觉得这本书略薄,没有包含进阶式的例子。<a href="http://www.weibo.com/n/otakustay">@otakustay</a>同学说:“确实,我的想法是在这之上补一个简单的MVC框架和一个StaticFile+Mimetype+CacheControl机制,可以成为一个更全面的教程”。正巧的是目前我手里的V5项目有一些特殊性:</p>',
"author": "Jackson",
- "time": '<abbr title="星期五, 十一月 11th, 2011, 5:03 下午">2011 年 11 月 11 日</abbr>'
+ "time": '<abbr title="星期五, 十一月 11th, 2011, 5:03 下午">2011 年 11 月 11 日</abbr>',
+ "who": "Jackson Tian",
+ "user": {"name": "Jackson Tian", "email": "shyvo1987@gmail.com"}
};
callback(data);
}, 100);
View
4 node_modules/footprint.js
@@ -1,4 +1,4 @@
-module.exports = footprint = {};
+var footprint = {};
// Default template settings, uses ASP/PHP/JSP delimiters, change the
// following template settings to use alternative delimiters.
var templateSettings = {
@@ -31,3 +31,5 @@ footprint.template = function (str, data) {
var compilied = footprint.compile(str);
return compilied(data);
};
+
+module.exports = footprint;
View
3  partials/comments.view
@@ -1 +1,2 @@
-Comments View
+Comments View
+<%=who%>
View
26 portal.js
@@ -14,14 +14,20 @@ Portal.templateSettings = {
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);
- var result = vm.runInNewContext(code, sandbox);
- //console.log(result);
- return result;
- });
+ try {
+ var temp = str.replace(c.interpolate, function (match, code) {
+ var script = "view.viewData." + code;
+ return vm.runInNewContext(script, sandbox);
+ //return sandbox.view.viewData[code] ? sandbox.view.viewData[code] : "The key " + code + " is undefined.";
+ }).replace(c.evaluate, function (match, code) {
+ console.log(code);
+ var result = vm.runInNewContext(code, sandbox);
+ //console.log(result);
+ return result;
+ });
+ } catch (ex) {
+ return ex.stack;
+ }
return temp;
};
@@ -55,9 +61,9 @@ Portal.prototype.dispatch = function (request, response) {
var portalView = new PortalView();
portalView.all("before", "file", function (viewData, file) {
console.log("Portal ready.");
+ portalView.viewData = viewData;
var sandbox = {
- "view": portalView,
- "viewData": viewData
+ "view": portalView
};
var preprocessed = Portal.preprocess(file, sandbox);
View
2  portals/post.portal
@@ -11,6 +11,8 @@
<div id="main">
<h2><%=title%></h2>
<section>
+ <%=user.name%>
+ <%=user.email%>
<%=content%>
</section>
<aside>By <%=author%> on <%=time%></aside>
View
12 portalview.js
@@ -2,6 +2,7 @@ var util = require("util");
var path = require("path");
var fs = require("fs");
var EventProxy = require("eventproxy").EventProxy;
+var footprint = require("footprint");
var PortalView = function () {
EventProxy.call(this);
@@ -19,6 +20,7 @@ util.inherits(PortalView, EventProxy);
* The view name will be replaced in postprocess phase.
*/
PortalView.prototype.partial = function (viewName, data) {
+ data = data || this.viewData;
this.partialSeq.push({"viewName": viewName, "viewData": data});
return "<%=" + viewName + "%>";
};
@@ -50,8 +52,10 @@ PortalView.prototype.getPartial = function (data) {
if (err) {
throw err;
} else {
- that.partialViews[data.viewName] = view.toString("utf-8");
- that.fire("partial_end", view.toString("utf-8"));
+ console.log(data.viewData);
+ var html = footprint.template(view.toString("utf-8"), data.viewData);
+ that.partialViews[data.viewName] = html;
+ that.fire("partial_end", html);
}
});
};
@@ -85,7 +89,9 @@ PortalView.prototype.processAjax = function (response) {
response.write(script);
});
};
-
+/**
+ * @description
+ */
PortalView.prototype.processPipes = function (response) {
// Process sequnences
this.pipes.forEach(function (script) {
Please sign in to comment.
Something went wrong with that request. Please try again.