Skip to content

Commit

Permalink
first work towards 'async' view helper
Browse files Browse the repository at this point in the history
  • Loading branch information
der-On committed Jun 29, 2015
1 parent 38cce76 commit 6e2207d
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
39 changes: 39 additions & 0 deletions lib/template/async_partial.js
@@ -0,0 +1,39 @@
(function() {
'use strict';

var isPromise = require('is-promise');

function AsyncPartial(handler, opts, parent) {
this.render = function(cb) {
var self = this;

if (typeof handler === 'function') {
handler(function (err, result) {
if (err) {
onError(err);
cb('');
return;
}

cb(result);
});
} else if (isPromise(handler)) {
handler
.then(function (result) {
cb(result);
})
.catch(function (err) {
onError(err);
cb('');
});
}

function onError(err) {
throw err;
}
}
}

exports.AsyncPartial = AsyncPartial;

}());
12 changes: 11 additions & 1 deletion lib/template/partial.js
Expand Up @@ -7,7 +7,8 @@
, path = require('path')
, fs = require('fs')
, utils = require('utilities')
, cache = {};
, cache = {}
, AsyncPartial = require('./async_partial').AsyncPartial;

Partial = function (templatePath, data, parent) {
var self = this;
Expand All @@ -26,6 +27,15 @@
self.addChild(child);
return '###partial###' + child.id
};

// Hang a `async` method on the execution-context for the
// template rendering (e.g., will be the EJS global `async`
// function to add async sub-templates
this.data.async = function (handler, opts) {
var child = new AsyncPartial(handler, opts, self);
self.addChild(child);
return '###partial###' + child.id;
};
};

Partial.prototype = new (function () {
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -13,6 +13,7 @@
"dependencies": {
"barista": "0.2.x",
"chalk": "^0.4.0",
"is-promise": "^2.0.0",
"jake": "8.0.x",
"mime": "1.2.x",
"model": "6.0.x",
Expand Down
58 changes: 58 additions & 0 deletions test/templates/async_partial.js
@@ -0,0 +1,58 @@
(function() {
'use strict';

require('../../lib/geddy');

var assert = require('assert')
, Partial = require('../../lib/template/partial').Partial
, tests;

// TODO: how to actually inject template code?

geddy.templateRegistry = {
'app/views/foo/baz': {
file: 'app/views/foo/baz.html.ejs'
, ext: '.ejs'
, baseName: 'baz'
, baseNamePath: 'app/views/foo/baz'
}

, 'app/views/foo/bar': {
file: 'app/views/foo/bar.html.ejs'
, ext: '.ejs'
, baseName: 'bar'
, baseNamePath: 'app/views/foo/bar'
}
};

geddy.viewHelpers.callbackHelper = function() {
return function(cb) {
cb(null, 'callback helper result');
};
};

geddy.viewHelpers.promiseHelper = function() {
return new Promise(function (resolve, reject) {
resolve('promise helper result');
});
};

tests = {

'callback helper': function () {
var p = new Partial('foo/bar', {})
, data = p.getTemplateData();
assert.ok(data);
}

, 'promise helper': function () {
var pParent = new Partial('app/views/foo/baz', {})
var pSub = new Partial('bar', {}, pParent)
, data = pSub.getTemplateData();
assert.ok(data);
}

};

module.exports = tests;
}());

2 comments on commit 6e2207d

@ckhatton
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@der-On #L28-L38 Is creating a build error. It seems 'geddy.viewHelpers' is outside the scope of the test.

I would comment them out, but I don't know how important they are to be included in the test.

TypeError: Cannot set property 'callbackHelper' of undefined
    at /Users/Crimbo/Documents/NodeJsProjects/geddy/test/templates/async_partial.js:28:36
    at Object.<anonymous> (/Users/Crimbo/Documents/NodeJsProjects/geddy/test/templates/async_partial.js:58:2)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:20:19)
    at /Users/Crimbo/.nvm/versions/node/v5.12.0/lib/node_modules/jake/lib/test_task.js:190:21
    at Array.forEach (native)

@ckhatton
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue #729 created.

Please sign in to comment.