[英]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元素的权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.