'visible' binding with arrays issue

已查看 36 次
跳至第一个未读帖子

Tomasz Płonka

未读,
2015年6月28日 06:54:232015/6/28
收件人 knock...@googlegroups.com
I have some JSON data returned from Ajax call that I first equip with the additional 'visible' field:
for (var index = 0; index < data.length; ++index) {
                    data
[index].visible = ko.observable(false);

Later on, I construct my model:
model = {
          jsonData
: ko.observableArray(data),

           clickClick
: function (place) {
                  place
.visible = true;
           
}
         
};

that is bound to the following:
<div id="map" class="map">
   
<!-- ko foreach: jsonData -->
   
<div class="marker" data-bind="attr: {'id': 'woeid' + $index()}">
       
<div>
           
<span data-bind="text: $data.name, click: $parent.clickClick"></span> (<span data-bind="text: $data.place_type"></span>): <span data-bind='text: $data.trends.length'>&nbsp;</span> trends
       
</div>
       
<ul data-bind="foreach: $data.trends, visible: $data.visible()">
           
<li>
               
<span data-bind="text: $data"></span>
           
</li>
       
</ul>
   
</div>
   
<!-- /ko -->

According to the initial value of the 'visible' field, lists are displayed or not, that's fine. However, clickClick() method changes the 'visible' field properly, but GUI is not updated. What am I doing wrongly?

Gunnar Liljas

未读,
2015年6月28日 17:37:082015/6/28
收件人 knock...@googlegroups.com
It's an observable, you set the value like this:

place.visible(true);

--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tomasz Płonka

未读,
2015年6月29日 03:00:142015/6/29
收件人 knock...@googlegroups.com
It's a function, got it.
But in this case, to track the value of the 'visible' I need another boolean property, or is there another way? In fact I want to toggle the value of 'visible'.

T.

Gunnar Liljas

未读,
2015年6月29日 03:05:122015/6/29
收件人 knock...@googlegroups.com
place.visible() returns the value 

place.visible(value) sets the value.

So if you want to toggle it:

place.visible(!place.visible())

Tomasz Płonka

未读,
2015年6月29日 03:07:012015/6/29
收件人 knock...@googlegroups.com
Ech, a function, you are right, thank you very much, thinking in c# properties at the moment.
回复全部
回复作者
转发
0 个新帖子