UNPKG

1.41 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
36 ? getTemplateData(newProps, out)
37 : newProps;
38
39 // Render the template associated with the component using the final template
40 // data that we constructed
41 template.render(templateData, out);
42 };
43 }
44
45 renderer.render = function(input) {
46 var out = createOut();
47 renderer(input, out);
48 return out.end();
49 };
50
51 return renderer;
52}
53
54module.exports = defineRenderer;