Knockout.JS可观察数组对单个可观察项目的更改

时间:2022-12-03 12:54:04

I have a view model with an observableArray (named 'all') of objects. One of the properties of that object is an observable name selected. I want some code to execute whenever the selected property of the of the child object in the array changes. I tried manually subscribing to all via all.subscribe() but that code only fires when items are added or removed. I updated the code to do it like this:

我有一个带有observableArray(名为'all')对象的视图模型。该对象的一个​​属性是选择了一个可观察的名称。我想要一些代码在数组中子对象的selected属性发生变化时执行。我尝试通过all.subscribe()手动订阅所有内容,但该代码仅在添加或删除项目时触发。我更新了代码,就像这样:

all.subscribe(function () {
    ko.utils.arrayForEach(all(), function (item) {
        item.selected.subscribe(function () {
            //code to fire when selected changes
        });
    });
});

Is this the right way to do this or is there a better way?

这是正确的方法吗?还是有更好的方法?

1 个解决方案

#1


20  

This is close to correct. Observable array subscriptions are only for when items are added or removed, not modified. So if you want to subscribe to an item itself, you'll need to, well, subscribe to the item itself:

这接近正确。可观察数组订阅仅适用于添加或删除项目而不进行修改的情况。因此,如果您想订阅一个项目本身,您需要订阅该项目本身:

Key point: An observableArray tracks which objects are in the array, not the state of those objects

关键点:observableArray跟踪数组中的对象,而不是这些对象的状态

Simply putting an object into an observableArray doesn’t make all of that object’s properties themselves observable. Of course, you can make those properties observable if you wish, but that’s an independent choice. An observableArray just tracks which objects it holds, and notifies listeners when objects are added or removed.

简单地将对象放入observableArray并不会使该对象的所有属性本身都可观察到。当然,如果您愿意,您可以观察这些属性,但这是一个独立的选择。 observableArray只跟踪它所拥有的对象,并在添加或删除对象时通知侦听器。

(from Knockout documentation)

(来自Knockout文档)


I say "close to correct" since you will want to remove all the old subscriptions. Currently, if the observable array starts as [a, b] you are subscribing to [a, b], but then if c gets added you have two subscriptions for a and b plus one for c.

我说“接近正确”因为您要删除所有旧订阅。目前,如果可观察数组以[a,b]开头,则您正在订阅[a,b],但如果添加了c,则您有两个订阅a和b加一个c。

#1


20  

This is close to correct. Observable array subscriptions are only for when items are added or removed, not modified. So if you want to subscribe to an item itself, you'll need to, well, subscribe to the item itself:

这接近正确。可观察数组订阅仅适用于添加或删除项目而不进行修改的情况。因此,如果您想订阅一个项目本身,您需要订阅该项目本身:

Key point: An observableArray tracks which objects are in the array, not the state of those objects

关键点:observableArray跟踪数组中的对象,而不是这些对象的状态

Simply putting an object into an observableArray doesn’t make all of that object’s properties themselves observable. Of course, you can make those properties observable if you wish, but that’s an independent choice. An observableArray just tracks which objects it holds, and notifies listeners when objects are added or removed.

简单地将对象放入observableArray并不会使该对象的所有属性本身都可观察到。当然,如果您愿意,您可以观察这些属性,但这是一个独立的选择。 observableArray只跟踪它所拥有的对象,并在添加或删除对象时通知侦听器。

(from Knockout documentation)

(来自Knockout文档)


I say "close to correct" since you will want to remove all the old subscriptions. Currently, if the observable array starts as [a, b] you are subscribing to [a, b], but then if c gets added you have two subscriptions for a and b plus one for c.

我说“接近正确”因为您要删除所有旧订阅。目前,如果可观察数组以[a,b]开头,则您正在订阅[a,b],但如果添加了c,则您有两个订阅a和b加一个c。