简体   繁体   English

当模型内部的属性更改时,如何更新DOM元素?

[英]How to update DOM element when property inside a model changed?

A model has a property called rectsList which contains a list of SVG rect elements (an arroy of rect elements). 模型具有称为rectsList的属性,该属性包含SVG rect元素列表(一系列rect元素)。

All the rects are append into an svg element in didRender lifecycle hook method (see code below), 所有的rects都将添加到didRender生命周期挂钩方法中的svg元素中(请参见下面的代码),

didRender() {
    let $svg = this.$().find('svg');
    this._clearSvg($svg);
    this._renderSelectedRects($svg); // append all rects in rectsList to svg
},

_renderSelectedRects($svg) {
    const rects = this.get('image.rectsList');
    rects.forEach((rect) => {
        $svg.append(rect);
    });
}

The expect behaviour: 预期的行为:

When the rectsList changes, the recent inside svg is automatically updated. rectsList更改时, recent内部svg会自动更新。

The question: 问题:

Where is the best place to re-render all the rect s on the DOM based on the current state of rectsList . 根据rectsList的当前状态,在DOM上重新呈现所有rect的最佳位置在哪里。

I have tried to log the state of the current model using didUpdateAttrs but when the rectsList changes the method is not get triggered. 我尝试使用didUpdateAttrs记录当前模型的状态,但是当rectsList更改时,不会触发该方法。

I am thinking to use the observer but I do not have the access to the DOM element inside callback function. 我正在考虑使用观察器,但是我没有访问回调函数内的DOM元素的权限。

You can render rects based on rectList without using observers or if any additional working needed, with using observers. 您可以基于rectList渲染rect,而无需使用观察器,或者如果需要任何其他工作,则可以使用观察器。

I provide a twiddle with both approaches. 我提供了两种方法的细微差别 Please take a look at it. 请看一下。

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

相关问题 当Polymer元素的bool属性更改时如何使用排序重新运行dom-repeat - How to re run dom-repeat with sort when bool property changed in Polymer element 设置VueJS模型属性不会更新DOM中的select元素 - Setting VueJS model property doesn't update select element in DOM 如何更新每个表单元素的状态以更改嵌套数据结构的正确属性,该数据结构是嵌套表单元素 DOM 的模型? - How to update at each form element's state change the correct property of a nested data-structure which is a model of the nested form element DOM? DOM元素的InnerHTML属性已更改,但未呈现 - InnerHTML property of DOM element changed, but not rendering 开玩笑在 DOM 链中测试属性更新 - Jest testing a property update inside a DOM chain 在Angular中更新模型后获取DOM元素 - Get DOM element after model update in Angular 当data- *属性更改时如何更新元素 - How to update element when data-* attribute changed 访问更改的DOM元素 - Accessing a changed DOM element 在WebRTC回调中更改属性时未触发Vue.js更新 - Vue.js update not triggered when property changed inside a WebRTC callback 在非角度事件中更改时,angularjs 1.6模型的两种方式绑定不会更新 - angularjs 1.6 two way binding of model doesn't update when changed inside a non angular event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM