KnockoutJS将ko.computed Array映射到observableArray

时间:2015-08-05 03:47:34

标签: javascript arrays knockout.js

正如问题所述,我正在尝试将一个数组结果从ko.computed转换为可观察数组。我这样做的原因是我想让我的数组中的所有对象都成为"可观察的"我试图解决的解决方案是将数组结果传递给不同的observableArray。我无法解释清楚所以这是我的代码(请参阅注释行以获得进一步说明):

//to make each objects observable
var DropDownHolder = function (itemID, itemName, amount, automatic) {
    var self = this;
    self.ID = ko.observable(deductionID);
    self.Name = ko.observable(deductionName);
    self.amount = ko.observable(amount);
    self.automatic = ko.observable(automatic);
};

//temporary list, creates a new filtered array -- no problem here
self.temporaryDropDown = ko.computed(function () {
    return ko.utils.arrayFilter(self.completeList(), function (completeItem) {
        var sameItem = false;

        ko.utils.arrayForEach(self.selectedItem(), function (pickedItem) {
            if (pickedItem.itemID() == completeItem.itemID()) {
                sameItem = true;
                return;
            }
        });
        return !sameItem;
    });
}, self);

//put unpicked in dropdown -- problem here
//cannot transfer contents of temporaryDropDown to DropDown
self.DropDown = ko.observableArray();
self.DropDown(ko.utils.arrayMap(self.temporaryDropDown(), function (dd) {
    return new DropDownHolder(dd.itemID, dd.itemName, dd.amount, dd.automatic);
}));

1 个答案:

答案 0 :(得分:0)

你的代码看起来很好,哪些应该可以正常工作

  

我相信<bean id="service1" parent="service"> <property name="url" value="${url1.property}" /> <property name="user" value="${user1.property}" /> </bean> <bean id="service2" parent="service"> <property name="url" value="${url2.property}" /> <property name="user" value="${user2.property}" /> </bean> 是计算出来的东西   两次,即初始加载,你得到[]以后获得实际数据。

找出使计算机不止一次发射的内容。