我有一个简单的Node.js模块,它接受Handlebars模板的目录,编译它们,然后导出一个键入其名称的对象:
'use strict';
var
fs = require('fs'),
path = require('path'),
handlebars = require('handlebars'),
templateDir = __dirname + '/templates/',
templates = {};
fs.readdirSync(templateDir).forEach(function (file) {
templates[path.basename(file, path.extname(file))] =
handlebars.compile(fs.readFileSync(templateDir + file));
});
module.exports = templates;
那么在其他模块中我可以:
var templates = require('./templates');
templates[SOME_TEMPLATE]({ ... });
我正在努力解决如何以异步方式执行此操作,特别是如何使用混合中的发射器进行导出。
var emitter = require('events').EventEmitter;
module.exports = function (callback) {
emitter.on('templates-compiled', function () {
callback(templates);
});
callback();
};
fs.readdir(templateDir, function (err, files) {
if (!err) {
files.forEach(function (file) {
fs.readFile(templateDir + file, function(err, data) {
if (!err) {
templates[path.basename(file, path.extname(file))] =
handlebars.compile(data);
if (files[files.length - 1] === file) {
emitter.emit('templates-compiled');
}
} else {
}
});
});
} else {
}
});
答案 0 :(得分:0)
Heres a modification of the above that works:
var EventEmitter = require('events').EventEmitter;
var path = require('path');
var fs = require('fs');
module.exports = function(dir, callback) {
var emitter = new EventEmitter();
var templates = {};
emitter.on('templates-compiled', function(t) {
callback(null, t);
});
emitter.on('templates-error', function(e) {
callback(e);
});
fs.readdir(dir, function(err, files) {
if (!err) {
files.forEach(function(file) {
fs.readFile(path.join(dir, file), function(err, data) {
if (!err) {
//note: i'm just calling toString() here - do your compile here instead
templates[path.basename(file, path.extname(file))] = data.toString();
if (files[files.length - 1] === file) {
emitter.emit('templates-compiled', templates);
}
} else {
emitter.emit('templates-error', err);
}
});
});
} else {
emitter.emit('templates-error', err);
}
});
};
However you may want to look at using q or async to trim down the pyramid of doom.