简体   繁体   English

javascript如何创建与DOM元素的单向数据绑定?

[英]How does javascript create one-way data binding with DOM elements?

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. 我不知道“单向数据绑定”是否实际上是正确的定义,但是我有一个名为timesheetRow的类,该类在被调用时代表表中的一行。

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. 该类具有一个称为generateRow()的方法,该方法将<td>元素绑定到对象属性,然后将所有这些元素附加到父<tr>元素。 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. 如果您看一下我添加到workHours元素中的事件侦听器,它可以直接在存储为我的对象属性之一的行上工作,但是更改该属性也会自动更改DOM。 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. 当workHours值小于1时,workProject select元素将被禁用。 I'm not directly changing the DOM, I'm changing my object. 我不是直接更改DOM,而是更改对象。

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! 在此先感谢您抽出宝贵时间回答我的问题!

this.row.querySelector('.workProject') returns a reference to a DOM Node, which is an object representing a DOM Node. this.row.querySelector('.workProject')返回对DOM节点的引用,该节点是表示DOM节点的对象。

disabled is a property on a select Node which manipulates the way the browser renders it. disabledselect节点上的一个属性,该属性控制浏览器呈现它的方式。 The rendering engine itself knows it needs to redraw the input when you change that disabled property. 更改该disabled属性时,呈现引擎本身知道需要重绘输入。

There is no magic involved. 没有魔术。

  1. you type something in the input, an input event is dispatched 您在输入中键入内容,将调度input事件

  2. you added an input listener on the input , so the listener gets called every time (1) happens 您在input上添加了input侦听input ,因此每次(1)发生时都会调用该侦听器

  3. your listener gets a reference to the select and changes a property on it 您的侦听器获得对select的引用并更改其属性

  4. the browser/thing responsible for rendering DOM is designed in such a way that changing that disabled property on select s does what one expects 负责呈现DOM的浏览器/事物的设计方式是,更改selectdisabled属性可以实现预期的功能

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM