是“条件渲染”吗?可以用纯JS吗?
我使用纯 JS 指令进行渲染:
http://beebole.com/pure/documentation /rendering-with-directives/
如果模板中的节点丢失,纯 JS 的默认行为是崩溃,因为:
模板中未找到节点“XXX”
此默认行为是完全可以理解的,因为它确保模板中不存在不一致。但在相同的情况下,人们希望跳过失败的分配并继续其余的分配(可能记录错误),以避免整个渲染因单个错误/拼写错误而失败。
有什么方法可以用纯 JS 获得这种行为吗?我可以告诉 Pure JS 仅渲染一个元素“如果它存在”吗?
I use Pure JS directives for rendering:
http://beebole.com/pure/documentation/rendering-with-directives/
If a node in the template is missing, the default behaviour of Pure JS is to crash due to:
The node "XXX" was not found in the template
This default behaviour is totally comprehensible, because it ensures there are no inconsistencies in the template. In same cases, though, one would like to skip a failing assignment and to proceed with the rest of the assignments (possibly logging the error), to avoid the whole rendering to fail because of a single error / typo.
Is there any way to obtain this behaviour with Pure JS? Can I tell Pure JS to render an element just "if it exists"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,当前稳定版本的Pure JS(修订版:2.79)不允许渲染一个元素“如果存在”?
以下纯代码片段显示了如何引发错误:
正如您所看到的,如果尚未找到
target
(通过 jQuery、dojo 等库之一),则会抛出上述错误。我在这种情况下使用的解决方法如下:
Unfortunately current stable version of Pure JS (revision: 2.79) does not allow to render an element "if it exists"?
The following Pure code snippet shows how the error is thrown:
As you can see, if
target
has not been found (by one of libraries such as jQuery, dojo etc.) then the mentioned error is thrown.Workaround I use in that kind of situations is as follows: