Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140817

Building different templates based on data type in javascript module

$
0
0

I am passing data to a javascript file to build HTML based on the data type of the object. I am then exporting the module to the other javascript file to generate an HTML file with that module's HTML widget.

Below is the code I am working with. I am sure I am missing something simple, but I cannot figure out what I am doing wrong. I am also interested in advice on how to optimize what I am trying to achieve. Thanks!

function createWidget(data) {
    switch(data.type) {
        case "logo":
            console.log(data.type);
            createLogoWidget(data);
            break;
        case "contact":
            console.log(data.type);
            createContactWidget(data);
            break;
        case "links":
            console.log(data.type);
            createLinksWidget(data);
            break;
        default:
    }
}
createLogoWidget = (data) => `
    <div>createLogoWidget</div>
    <div>${data.type}</div>
`;
createContactWidget = (data) => `
    <div>createContactWidget</div>
    <div>${data.type}</div>
`;
createLinksWidget = (data) => `
    <div>createLinksWidget</div>
    <div>${data.type}</div>
`;
module.exports = createWidget;

Viewing all articles
Browse latest Browse all 140817

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>