-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetContent.js
More file actions
68 lines (61 loc) · 1.88 KB
/
getContent.js
File metadata and controls
68 lines (61 loc) · 1.88 KB
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
var EventProxy = require("./eventProxy.js");
var fs = require("fs")
exports.getContent = function(callback) {
console.log("run content")
var ep = new EventProxy();
ep.all('tpl', 'data', function(tpl, data) {
// 成功回调
callback(null, {
template: tpl,
data: data
});
});
// 侦听error事件
ep.bind('error', function(err) {
// 卸载掉所有handler
ep.unbind();
// 异常回调
callback(err);
});
fs.readFile('template.tpl', 'utf-8', function(err, content) {
if (err) {
// 一旦发生异常,一律交给error事件的handler处理
return ep.emit('error', err);
}
// console.log('读取模板', content)
ep.emit('tpl', content);
});
fs.readFile('data.json', 'utf-8', function(err, content) {
if (err) {
// 一旦发生异常,一律交给error事件的handler处理
return ep.emit('error', err);
}
ep.emit('data', content);
});
// db.get('some sql', function(err, result) {
// if (err) {
// // 一旦发生异常,一律交给error事件的handler处理
// return ep.emit('error', err);
// }
// ep.emit('data', result);
// });
};
exports.getContent2 = function(callback) {
var ep = new EventProxy();
ep.all('tpl', 'data', function(tpl, data) {
// 成功回调
callback(null, {
template: tpl,
data: data
});
});
// 添加error handler
ep.fail(callback);
fs.readFile('template.tpl', 'utf-8', ep.done('tpl'));
// fs.readFile('data.json', 'utf-8', ep.done('data'));
fs.readFile('data.json', 'utf-8', ep.done(function(content) {
ep.emit("data", content)
}));
// ep = new EventProxy();
//db.get('some sql', ep.done('data'));
};