Permalink
Browse files

Port asyncEJS to node 0.30

  • Loading branch information...
1 parent 405cfbf commit 2a8e155c94d4afe22787c14e42a2329c9ef0a8bc Malte Ubl committed Mar 1, 2010
Showing with 66 additions and 35 deletions.
  1. +39 −8 lib/asyncEJS.js
  2. +27 −27 test/test.js
View
@@ -1,7 +1,38 @@
var path = require('path'),
- posix = require('posix'),
+ fs = require('fs'),
sys = require('sys');
+function Promise() {
+ this.cbs = [];
+ this.errbs = [];
+}
+
+Promise.prototype = {
+ addCallback: function (cb) {
+ this.cbs.push(cb);
+ },
+ addErrback: function (errb) {
+ this.errbs.push(errb)
+ },
+ emitSuccess: function () {
+ for(var i = 0, len = this.cbs.length; i < len; ++i) {
+ var cb = this.cbs[i];
+ cb.apply(this, arguments);
+ }
+ },
+ emitError: function (err) {
+ var called = false;
+ for(var i = 0, len = this.errbs.length; i < len; ++i) {
+ var cb = this.errbs[i];
+ cb.call(this, err);
+ called = true;
+ }
+ if(!called) {
+ throw(err)
+ }
+ }
+}
+
var templateCache = {};
// A TemplateEngine holds template factories
@@ -19,16 +50,16 @@ exports.Engine = function (config) {
TemplateEngine.prototype = {
- // Synchronous template constructor
- template: function (filename) {
- return this.templateAsync(filename).wait();
+ // template constructor with a callback parameter
+ template: function (filename, cb) {
+ this.templateAsync(filename).addCallback(cb);
},
// Asynchronous template constructor
// Returns a promise for a template
templateAsync: function (filename) {
var self = this;
- var templatePromise = new process.Promise();
+ var templatePromise = new Promise();
if (filename.charAt(0) !== '/') {
filename = path.join(this.templateRoot, filename);
@@ -74,7 +105,7 @@ TemplateEngine.prototype = {
// Should not be public :)
partial: function (filename, res, ctx) {
- var partialPromise = new process.Promise();
+ var partialPromise = new Promise();
var p = this.templateAsync(filename);
var partRes;
@@ -105,8 +136,8 @@ TemplateEngine.prototype = {
};
function load(filename, cb, errb) {
- posix.cat(filename).addCallback(function (str) {
- //sys.puts(str.toString());
+ fs.readFile(filename, function (err, str) {
+ if (err) throw err;
var src = parseAndGenerate(str);
View
@@ -1,47 +1,47 @@
var assert = require('assert'),
sys = require("sys"),
path = require('path'),
- posix = require("posix");
+ fs = require("fs");
-var reference = posix.cat(path.join(__dirname, "fixtures/reference.html")).wait();
+var reference = fs.readFileSync(path.join(__dirname, "fixtures/reference.html"));
(function testRender() {
var te = require("../lib/asyncEJS").Engine();
- var t1 = te.template("fixtures/template.js.html");
+ te.template("fixtures/template.js.html", function (t1) {
+ var templateResponse = t1({
+ hello: []
+ });
+ var body = ""
+ templateResponse.addListener("body", function (chunk) {
+ body += chunk;
+ sys.print(chunk);
+ });
- var templateResponse = t1({
- hello: []
- });
- var body = ""
- templateResponse.addListener("body", function (chunk) {
- body += chunk;
- sys.print(chunk);
- });
-
- templateResponse.addListener("complete", function () {
- if(body === reference) {
- sys.puts("testRender: OK");
- } else {
- sys.puts("testRender: NOT OK")
- }
- });
+ templateResponse.addListener("complete", function () {
+ if(body === reference) {
+ sys.puts("testRender: OK");
+ } else {
+ sys.puts("testRender: NOT OK")
+ }
+ });
+ })
})();
(function testTemplateRoot() {
var te = require('../lib/asyncEJS').Engine({templateRoot: path.join(__dirname, 'fixtures')});
- var template = te.template('template_root_test.js.html');
- var templateResponse = template({ hello: [] });
- var body = ""
+ te.template('template_root_test.js.html', function (template) {
+ var templateResponse = template({ hello: [] });
+ var body = ""
- templateResponse.addListener('body', function (c) { body += c; });
+ templateResponse.addListener('body', function (c) { body += c; });
- process.addListener('exit', function () {
- assert.equal(body, reference);
- sys.puts('testTemplateRoot: passed');
+ process.addListener('exit', function () {
+ assert.equal(body, reference);
+ sys.puts('testTemplateRoot: passed');
+ });
});
-
})();

0 comments on commit 2a8e155

Please sign in to comment.