目前我有一个类似于以下计算的可观察值:
// Backed with either an observable array or an observable of an array
var underlying = ko.observable([..]);
var obs = ko.computed({
read: function () { return underlying(); },
write: function (items) {
// Process items - basically, I have the parent collection quickly
// subscribe to an observable on each item. This in and of itself
// should likely be cleaned up, but is not the focus of this question.
// For instance:
items.forEach(function (i) {
if (!subscribed(i.title)) {
i.title.subscribe(removeItemWhenEmptyTitle);
}
});
underlying(items);
}
});
但是,我希望能够将这个计算出的 observable 视为一个observable 数组,以便我可以调用obs.push(..)
等等。
破解这个有点微不足道,但感觉不对,我不想复制所有现有的可观察数组方法。
obs.push = function (item) {
var arr = obs();
arr.push(item);
obs(arr);
});
此外,我可能会遗漏可观察数组和可观察数组之间的关键区别。