0

I don't know if "one-way data binding" is actually the correct definition, but I have a class called timesheetRow, which represents a row in a table when called.

The class has a method called generateRow(), which binds <td> elements to the object properties and then appends all of them to a parent <tr> element. I do it this way because before returning the row, I gotta add some event listeners for validation purposes. The code is like this:

class timesheetRow{

    generateRow(){

        this.row = htmlToElement(`
            <tr>
            </tr>
        `);

        this.nodes.workHours = htmlToElement(`
            <td>
                <input name="workHours" 
                min=0 
                max=24 
                class="form-control workHours" 
                type="number">
            </td>
        `);

        this.nodes.workProject = htmlToElement(`
            <td>
                <select name="workProject" 
                class="form-control workProject" 
                required>
                    <option disabled selected value="">Sin proyecto asignado</option>
                    <option value="1">Proyecto 1</option>
                </select>
            </td>
        `);

        this.nodes.workHours.addEventListener("input", event => {
            this.row.querySelector('.workProject').disabled = parseInt(event.target.value) < 1;
        });

        for (let node in this.nodes) {
            this.row.appendChild(this.nodes[node]);
        }

        return this.row;
    }
}

This is a very abridged version of the actual code, but I hope it will work to clarify my question.

If you take a look at the event listener I added to the workHours element, it works directly on the row stored as one of my object properties, but changing that property automatically changes the DOM as well. So, as far as I know, that looks like one-way data binding. When the workHours value is below 1, the workProject select element gets disabled. I'm not directly changing the DOM, I'm changing my object.

I would like to know what's going on behind the scenes here. Thanks in advance to anyone who takes the time to answer my question!

3
  • What's htmlToElement? Commented Mar 19, 2019 at 13:23
  • A helper function I created to convert a string to an element using templates. @SergiuParaschiv Commented Mar 19, 2019 at 13:24
  • 1
    There is no binding going on here, it's just your storing a reference to the Dom nodes,.. Commented Mar 19, 2019 at 13:30

1 Answer 1

1

this.row.querySelector('.workProject') returns a reference to a DOM Node, which is an object representing a DOM Node.

disabled is a property on a select Node which manipulates the way the browser renders it. The rendering engine itself knows it needs to redraw the input when you change that disabled property.

There is no magic involved.

  1. you type something in the input, an input event is dispatched

  2. you added an input listener on the input, so the listener gets called every time (1) happens

  3. your listener gets a reference to the select and changes a property on it

  4. the browser/thing responsible for rendering DOM is designed in such a way that changing that disabled property on selects does what one expects

Also this.row.querySelector('.workProject') !== this.nodes.workProject.

Sign up to request clarification or add additional context in comments.

7 Comments

Isn't this.row.querySelector('.workProject') a reference to the object's row property?(which is a virtual DOM element). It is not referencing the dom itself as document.querySelector() would do.
Right, but "virtual DOM" is still DOM like your main document DOM, nothing special about it in that way. And the moment you import it in your "main" DOM it's part of it.
So we could say that adding a virtual DOM element to the "main" DOM creates a one-way data binding relation?
No. Has nothing to do with data binding. Data binding is something UI framework people named a feature, there is no such thing defined by DOM/browsers. What you have here is basic DOM manipulation and events with the added complexity of "virtual DOM" which in your case is only an easy way to generate DOM fragments. But yes, no one is stoping you from calling it anything :)
So to be more technical, the "main" DOM element somehow points to my object's row property and is aware of any changes it gets, to be updated right?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.