Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lit-element without webcomponent #27

Open
mustafaekim opened this issue Jan 25, 2018 · 0 comments
Open

lit-element without webcomponent #27

mustafaekim opened this issue Jan 25, 2018 · 0 comments

Comments

@mustafaekim
Copy link

mustafaekim commented Jan 25, 2018

Hi,

I was trying to implement something similar but without webcomponents. A simple function that renders some templateResult, having its own variables and functions.. I came up with a function like this

dummy.component.js:

import { html } from 'lit-html';
export default function dummyComponent(updateHandler) {
    console.log("Dummy componenting...");

    var counter = 0;

    function increment(){
        console.log("incrementing..")
        counter++;

        updateHandler();
    }

    var templateResult = html`
        Hello ${name}, I am dummy @ ${counter}
        <button type="button" onclick="${()=>{increment()}}">Increment</button>
    `

    return templateResult;
}

And to render it, I simply put it inside another templateResult

ui.js:

import { html } from 'lit-html';

import dummyComponent from './components/dummy.component.js';

var rootElement;
var state;
var templateResult;


function updateTemplateResult() {
    console.log("Updating template result...");

    templateResult = html`
        Hello ${state.name},

        ${dummyComponent(update)}
    `;
}

function update() {
    updateTemplateResult();
    render(templateResult, rootElement);
}

export var ui = {
    setRootElement: function (rootEl) {
        rootElement = rootEl;
        return ui;
    }
    , setState: function (st) {
        state = st;
        return ui;
    }
    , display: function () {
        update();
    }
}

and finally
app.js

import { router } from "./router.js"
import { init, rerender } from "./render.js"

import { ui } from "./ui.js";

var uiElement = document.getElementById("ui");
ui
    .setRootElement(uiElement)
    .setState({ name: "Mustafa" })
    .display();

this does not work because everytime updateTemplateResult is called, the dummycomponent is re-created and hence the counter goes back to zero.

is this the kind of problem to use webcomponents instread of simple javascript functions to create reusable components?

I know that is not an issue with your library, I just wanted to try my chance! Thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant