marko/widgets/defineRenderer.js
2016-12-13 11:34:26 -07:00

37 lines
1.1 KiB
JavaScript

var marko = require('../');
var makeRenderable = require('../runtime/renderable');
module.exports = function defineRenderer(renderingLogic) {
var renderer = renderingLogic.renderer;
if (renderer && renderer._isRenderer) {
return renderer;
}
var template = renderingLogic.template;
if (typeof template === 'string') {
template = marko.load(template);
}
if (!renderer) {
// Create a renderer function that takes care of translating
// the input properties to a view state. Also, this renderer
// takes care of re-using existing widgets.
renderer = function renderer(input, out) {
// Render the template associated with the component using the final template
// data that we constructed
template._(input, out, renderingLogic);
};
}
renderer._isRenderer = true;
renderer.createOut = template ? template.createOut : renderingLogic.createOut;
renderer.template = template;
makeRenderable(renderer, renderer);
return renderer;
};