Created
April 23, 2011 14:30
-
-
Save bergmark/938647 to your computer and use it in GitHub Desktop.
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
var Options = Cactus.Util.Options; | |
var jade = require("jade"); | |
var C = Cactus.Data.Collection; | |
Class("CpsJade", { | |
has : { | |
layoutFile : { required : true } | |
}, | |
trait : JooseX.CPS, | |
methods : { | |
_parseArgs : function (args) { | |
return new Options({ | |
type : { | |
res : { type : Object }, | |
layoutLocals : { map : true, type : "mixed", defaultValueFunc : function () { return {}; } }, | |
partials : { | |
defaultValueFunc : function () { return {}; }, | |
map : true, | |
type : { | |
filename : { type : "string" }, | |
variableName : { type : "string", required : false }, | |
collection : { type : [{ type : "mixed" }], required : false }, | |
locals : { | |
defaultValueFunc : function () { return {}; }, | |
map : true, | |
type : "mixed" | |
} | |
} | |
}, | |
templateFile : { type : "string" }, | |
templateLocals : { map : true, type : "mixed", defaultValueFunc : function () { return {}; } } | |
} | |
}).parse(args); | |
} | |
}, | |
continued : { | |
methods : { | |
renderFile : function (templateFile, options) { | |
var cont = this.CONT.getCONTINUE(); | |
jade.renderFile(templateFile, options, function (e, html) { | |
if (e) { | |
console.log("error rendering", templateFile, options); | |
console.log(e); | |
throw e; | |
} | |
cont(html); | |
}); | |
}, | |
renderWithLayout : function (args) { | |
var me = this; | |
args = this._parseArgs(args); | |
args.templateLocals.partials = {}; | |
var partials = args.partials; | |
var cont = new JooseX.CPS.Continuation(); | |
for (var name in partials) { | |
var variableName = partials[name].variableName; | |
var filename = partials[name].filename; | |
var collection = partials[name].collection; | |
var locals = partials[name].locals || {}; | |
if (!collection) { | |
cont.and(function (filename, name, locals) { | |
me.renderFile(filename, { | |
locals : locals | |
}).then(function (html) { | |
args.templateLocals.partials[name] = html; | |
this.CONTINUE(); | |
}).now(); | |
}.curry(filename, name, locals)); | |
} else if (collection) { | |
args.templateLocals.partials[name] = ""; | |
C.each(collection, function (collectionItem) { | |
var os = { | |
locals : Joose.O.copy(locals) | |
}; | |
os.locals[variableName] = collectionItem; | |
cont.and(function (filename, name) { | |
me.renderFile(filename, os).then(function (html) { | |
args.templateLocals.partials[name] += html; | |
this.CONTINUE(); | |
}).now(); | |
}.curry(filename, name)); | |
}); | |
} | |
} | |
cont.then(function () { | |
me.renderFile(args.templateFile, { | |
locals : args.templateLocals | |
}).now(); | |
}).then(function (html) { | |
args.layoutLocals.content = html; | |
me.renderFile(me.layoutFile, { | |
locals : args.layoutLocals | |
}).now(); | |
}).then(function (html) { | |
args.res.send(html); | |
}).now(); | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment