forked from KillerCodeMonkey/NoMuRe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.js
142 lines (115 loc) · 4.15 KB
/
render.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*jslint node:true, vars:true, nomen:true*/
'use strict';
var MustacheEngine = require('mu2');
var fs = require('fs');
var _ = require('underscore');
var promise = require('node-promise');
var basecontroller = require('./controller/base/view');
var productcontroller = require('./controller/product/view');
var categorycontroller = require('./controller/category/view');
var controllers = {
'product/view': productcontroller,
'category/view': categorycontroller,
'base/view': basecontroller
};
var Promise = promise.Promise;
var clearCache = function () {
MustacheEngine.clearCache();
};
var renderTemplate = function (template, data, callback) {
fs.stat(template, function (err, stat) {
if (err || !stat.isFile()) {
return callback(err || template + ' is no file.');
}
var stream = MustacheEngine.compileAndRender(template, data);
var result = '';
stream.on('data', function (data) {
result += data;
});
stream.on('end', function () {
callback(null, result);
});
});
};
var render = function (type, view, mode, data, callback) {
var controllername = type + '/' + view;
var controller = controllers[controllername];
var base = controller.base;
if (base) {
var basecontrollername = base.controller;
var baseview = base.view || 'view';
render(basecontrollername, baseview, mode, data, function (err, basecontent) {
if (err) {
return callback(err);
}
var tasks = _.map(base.extensions, function (extensiontemplate, extensionname) {
var task = new Promise();
var template = './templates/' + type + '/' + extensiontemplate + '.' + mode;
renderTemplate(template, data, function (err, extensioncontent) {
if (err) {
return task.reject(err);
}
// replace extensions in basecontent
basecontent = basecontent.replace('{*' + extensionname + '*}', extensioncontent);
task.resolve();
});
return task;
});
promise.all(tasks).then(function () {
callback(null, basecontent);
}, callback);
});
} else {
var template = './templates/' + type + '/' + view + '.' + mode;
return renderTemplate(template, data, callback);
}
};
var getData = function (type, view, mode, params, callback) {
var controllername = type + '/' + view;
var controller = controllers[controllername];
if (!controller) {
return callback('Unknown controller type "' + controllername + '".');
}
var currentcontroller = controller;
var basecontrollername, basecontroller, base, datatask;
var tasks = [];
var createCallback = function (datatask) {
return function (err, data) {
if (err) {
return datatask.reject(err);
}
datatask.resolve(data);
};
};
while (currentcontroller) {
datatask = new Promise();
tasks.push(datatask);
currentcontroller.getData.apply(currentcontroller, params.concat(createCallback(datatask)));
base = currentcontroller.base;
if (base) {
basecontrollername = base.controller + '/' + (base.view || 'view');
basecontroller = controllers[basecontrollername];
} else {
basecontroller = undefined;
}
currentcontroller = basecontroller;
}
promise.all(tasks).then(function (results) {
var data = _.extend.apply(_, results.reverse());
callback(null, data);
}, callback);
};
var display = function (type, view, mode, params, callback) {
getData(type, view, mode, params, function (err, data) {
if (err) {
return callback(err);
}
if (mode === 'json') {
var result = JSON.stringify(data);
return callback(null, result);
}
render(type, view, mode, data, callback);
});
};
exports.render = display;
exports.clearCache = clearCache;