forked from JacksonTian/ping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
portalview.js
executable file
·113 lines (100 loc) · 3.17 KB
/
portalview.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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);
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) {
data = data || this.viewData;
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 {
console.log(data.viewData);
var html = footprint.template(view.toString("utf-8"), data.viewData);
that.partialViews[data.viewName] = html;
that.fire("partial_end", html);
}
});
};
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);
});
};
/**
* @description
*/
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;