简体   繁体   English

淘汰赛:从null开始observableArray

[英]Knockout: start observableArray with null

Seems that in knockout 3.2.0 they changed the behavior of ObservableArrays. 似乎在淘汰赛3.2.0中,他们更改了ObservableArrays的行为。 In knockout 2, if I did: 在淘汰赛2中,如果我这样做了:

var array = ko.observableArray(null)
console.log(array())

It would return me null. 它将返回我为空。 The same thing on knockout 3.2.0 doesn't happen because the observable array instead of null is create as an empty array. 在敲除3.2.0上不会发生相同的事情,因为可观察的数组而不是null被创建为空数组。

This is my case: 这是我的情况:

<div>
 <div class="spinner" data-bind="visible: comments() == null">
 <!-- ko foreach: comments -->
 ...
 <!-- /ko -->
</div>

I'd like to start showing in the comments div a spinner, and when the comments are populated, i'll hide the spinner and show the comments. 我想开始在注释div中显示一个微调器,并且在填充注释时,我将隐藏微调器并显示注释。 I can't do data-bind="visible: comments().length == 0" because if the post has no comments, the comments array will have 0 length and the spinner will be shown forever. 我无法执行data-bind="visible: comments().length == 0"因为如果帖子中没有评论,则comments数组的长度将为0,并且旋转器将永远显示。

How can I make this work? 我该如何进行这项工作?

This would work. 这会起作用。

self.comments = ko.observableArray([]);

<!-- ko if: comments().length > 0 -->
Greater than zero // Show spinner
<!-- /ko -->
<!-- ko ifnot: comments().length > 0 -->
No greater than zero // No spinner needed
<!-- /ko -->

Let me know what you think. 让我知道你的想法。

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

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