在什么条件下会重新渲染Knockout本机模板 [英] Under what conditions does a Knockout native template re-render

查看:62
本文介绍了在什么条件下会重新渲染Knockout本机模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复杂的剔除页面,可以有条件地呈现模板:

I have a complex knockout page that renders a template conditionally:

<!-- ko template: {'if': $root.itemToEdit.SomeObject() === $data, name: 'EditItemTemplate', afterRender: $root.initializeEditPanel } -->
<!-- /ko -->

和模板:

<script type="text/html" id="EditItemTemplate">
    <div id="editContainer" class="fd_editContainer">
       //.. lots of markup and knockout bindings ...
       <input class="checkbox" id="questionDisplayOptionOverride" type="checkbox" data-bind="checked: $data.AnObject().ItemText.HasOverrideText" />
       //.. lots of markup and knockout bindings ...
     </div>
</script>

这就是让我感到困惑的地方.标记中有一堆元素会更改$data对象的属性.这些不会导致模板重新呈现.但是,由于某些原因,当单击某个复选框(questionDisplayOptionOverride)时,将重新呈现整个模板,并调用我的afterRender函数$root.initializeEditPanel.我不知道为什么会这样,因为questionDisplayOptionOverride控件仅更改$data对象内部的computedObservable属性,而不更改实际的$data对象本身.

Here is what is confusing me. There are a bunch elements in the markup that alter properties on the $data object. These do not cause the template to re-render. However, for some reason, when a certain checkbox is clicked (questionDisplayOptionOverride) the full template is re-rendered and my afterRender function $root.initializeEditPanel is called. I do not know why this is happening as the questionDisplayOptionOverride control only alters a computedObservable property inside the $data object, not the actual $data object itself.

所以我的问题:

在什么条件下可以完全重新渲染模板?

很明显,如果模板条件'if': $root.itemToEdit.SomeObject() === $data更改了,模板将重新呈现,但是在其他条件下会发生这种情况吗?

Clearly if the template condition 'if': $root.itemToEdit.SomeObject() === $data changed the template would re-render but are there any other conditions under which this would happen?

由于我页面的复杂性,因此无法选择jsFiddle. 我对导致模板重新呈现的通用机制更感兴趣.

Due to the complexity of my page a jsFiddle is not an option. I am more interested in the general mechanism that causes templates to re-render.

请参见 afterRender模板调用似乎被执行为computedObservables.为什么以及如何解决?作为对此的后续问题.

See The afterRender template call seems to be executed as a computedObservables. Why and how to fix it? as a follow up question to this.

推荐答案

每当$root.itemToEdit.SomeObject更改时,您使用的模板将重新呈现.如果它包含一个对象,则仅更改其一个属性是不够的.整个可观察的对象都需要更新,以便发生通知.

The template in your case would re-render whenever $root.itemToEdit.SomeObject changed. If it contains an object, then it is not good enough for just one of its properties to be changed. The entire observable needs to be updated for notifications to happen.

这是重新渲染模板的唯一方法,除了诸如由于父模板而导致重新渲染之类的东西.

That is the only way that your template would get re-rendered except for something like getting re-rendered because of a parent template.

这篇关于在什么条件下会重新渲染Knockout本机模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆