UNPKG

1.39 kBJavaScriptView Raw
1var marko = require("../");
2
3function defineRenderer(def) {
4 var template = def.template;
5 var getTemplateData = def.getTemplateData;
6 var renderer = def.renderer;
7
8 if (typeof template === "string") {
9 template = marko.load(template);
10 }
11
12 var createOut;
13
14 if (template) {
15 createOut = template.createOut;
16 } else {
17 createOut = def.createOut || marko.createOut;
18 }
19
20 if (!renderer) {
21 // Create a renderer function that takes care of translating
22 // the input properties to a view state. Also, this renderer
23 // takes care of re-using existing components.
24 renderer = function renderer(input, out) {
25 var newProps = input;
26
27 if (!newProps) {
28 // Make sure we always have a non-null input object
29 newProps = {};
30 }
31
32 // Use getTemplateData(state, props, out) to get the template
33 // data. If that method is not provided then just use the
34 // the state (if provided) or the input data.
35 var templateData = getTemplateData ? getTemplateData(newProps, out) : newProps;
36
37 // Render the template associated with the component using the final template
38 // data that we constructed
39 template.render(templateData, out);
40 };
41 }
42
43 renderer.render = function (input) {
44 var out = createOut();
45 renderer(input, out);
46 return out.end();
47 };
48
49 return renderer;
50}
51
52module.exports = defineRenderer;
\No newline at end of file