Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first work towards 'async' view helper
- Loading branch information
Showing
4 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}()); |
6e2207d
There was a problem hiding this comment.
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.
6e2207d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Issue #729 created.