在挖掘可观察的内部使用数组

时间:2013-04-01 11:01:15

标签: knockout.js observable ko.observablearray

我有可观察的数组,它们绑定到UI上的html字段。

但是,UI上这些字段的更改不会反映在viewmodel中,因为根据Knockout文档,observableArray会跟踪数组中的对象,而不是这些对象的状态。

代码如下所示:

var RewardMissionModel = function () {
    var self = this;
    self.achievementID = ko.observable();
    self.rewardAsset = ko.observable();
    self.rewards = ko.observableArray([new Reward("Points", "Qty", "", "Item", "")]);

    self.isDirty = false;

    //Add reward
    self.addReward = function () {
        //var self = this;
        var tempRewardType = "Points";
        self.rewards.push(new Reward(tempRewardType, "Qty", "", "Item", "", 0));
    };

    //Remove reward
    self.removeReward = function (reward) {
        if (self.rewards().length > 1)
            self.rewards.remove(reward);
    };
    //------------------------------------------------------------------------------
};

function Reward(rewardType, quantityLabel, rewardQuantity, itemLabel, item, itemID) {
    var self = this;
    //self.viewModel = viewModel;
    self.rewardTypes = ["Points", "Coins", "Items"];    
    self.rewardType = ko.observable(rewardType);
    self.itemLabel = ko.observable(itemLabel);
    self.itemText = ko.observable(item);
    self.quantityLabel = ko.observable(quantityLabel);
    self.rewardQuantity = ko.observable(rewardQuantity);
    this.selectedRewardType = ko.observable(rewardType);
    self.isItemType = function (selectedRewardType) {
        return selectedRewardType === this.selectedRewardType();
    }

    self.itemID = ko.observable(itemID);

}

HTML code:

<div data-bind="template: { name: 'rewards-template', foreach: rewards }"></div>
                <script type="text/html" id="rewards-template">
                    <span style="margin-left:30px">Reward Type</span>
                    <select class="dropdown" data-bind="options: rewardTypes, value: selectedRewardType"></select>
                    <span data-bind="text: itemLabel, visible: isItemType('Items')"></span>

当我在observable数组中添加或删除对象时,viewmodel脏标志被设置,但是当我在UI上修改时,它不被考虑。

其中一种方法是将一个普通数组放在一个可观察的数组中。

是否有任何代码示例来说明这种或更好的方法?

请帮忙。

2 个答案:

答案 0 :(得分:0)

我认为使可观察数组对象的属性充当可观察性的最佳方法是使它们也是可观察的,这样你就可以创建包含observable的可观察数组。例如::

    var viewModel = {
    accounts: ko.observableArray([]),
    init: function () {
        this.accounts.push({
            Property1: ko.observable("some value"),
            Property2: ko.observable("some value"),
            Property3: ko.observable("some value")
            });
        }
    };

请仔细检查这个问题,我认为这就是你要找的东西

Updating of object inside observable array is done but changes are not coming on browser

答案 1 :(得分:0)

function MyObject(val1, val2)
{
   this.firstProperty = ko.observable(val1);  
   this.secondProperty = ko.observable(val1);
}  

var myViewModel = new function()  
{  
   var self = this;
   self.values = ko.observableArray([]);  
   self.values.push(new MyObject("value11", "value12"));  
   self.values.push(new MyObject("value21", "value22"));
}