使用 ko.utils.arrayForEach 迭代 observableArray

发布于 2025-01-06 21:51:56 字数 866 浏览 0 评论 0原文

我正在尝试计算“observableArray”的“价格”字段的总和。到目前为止,我有以下代码:

(function(){

function objFeatures(name,price) {
        return {
            name: ko.observable(name),
            price: ko.observable(price),

            removeFeatures: function () {
                appViewModel.features.remove(this);
            }
        }
    }

var appViewModel = {
features: ko.observableArray([
            new objFeatures("Feature1", 20),
            new objFeatures("Feature2", 20)
        ]),

 grandTotal: ko.computed(function () {
            var total = 0;
            ko.utils.arrayForEach(this.features(), function () {
                total += this.price();
            })
            return total;
        })
};

ko.applyBindings(appViewModel);

}());

当我尝试运行此代码时,我在 firebug 控制台中收到“错误:this.features 不是函数”

我做错了什么?

I am trying to calculate sum of 'price' field of an 'observableArray'. I have the following code so far:

(function(){

function objFeatures(name,price) {
        return {
            name: ko.observable(name),
            price: ko.observable(price),

            removeFeatures: function () {
                appViewModel.features.remove(this);
            }
        }
    }

var appViewModel = {
features: ko.observableArray([
            new objFeatures("Feature1", 20),
            new objFeatures("Feature2", 20)
        ]),

 grandTotal: ko.computed(function () {
            var total = 0;
            ko.utils.arrayForEach(this.features(), function () {
                total += this.price();
            })
            return total;
        })
};

ko.applyBindings(appViewModel);

}());

When I try to run this, i get an "Error: this.features is not a function" in the firebug console.

What am i doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌酱 2025-01-13 21:51:56

计算的可观察量在创建期间立即评估。在您的情况下,appViewModel 尚未创建,this 将不代表 appViewModel

在这种情况下,有很多方法可以确保 this 是正确的。这里有两个:

  1. 在初始对象文字之外创建它:

    var appViewModel = {
       特征: ko.observableArray([
           新的 objFeatures("特征 1", 20),
           新的 objFeatures("特征2", 20)
           ])
    };
    
    appViewModel.grandTotal = ko.compulated(function() {
        总变量 = 0;
        ko.utils.arrayForEach(this.features(), 函数(feature) {
            总计 += feature.price();
        });
    
        返回总计;
    }、appViewModel);
    
  2. 在函数中创建视图模型:

    var AppViewModel = function() {
        this.features = ko.observableArray([
            新的 objFeatures("特征 1", 20),
            新的 objFeatures("特征2", 20)
        ]);
    
        this.grandTotal = ko.compulated(function() {
            总变量 = 0;
            ko.utils.arrayForEach(this.features(), 函数(feature) {
                总计 += feature.price();
            });
            返回总计;
        }, 这);
    };
    
    ko.applyBindings(new AppViewModel()); 
    

Computed observables are evaluated immediately during creation. In your case, appViewModel has not been created yet and this will not represent the appViewModel.

There are many ways to ensure that this is correct in this case. Here are two:

  1. Create it outside of your initial object literal:

    var appViewModel = {
       features: ko.observableArray([
           new objFeatures("Feature1", 20),
           new objFeatures("Feature2", 20)
           ])
    };
    
    appViewModel.grandTotal = ko.computed(function() {
        var total = 0;
        ko.utils.arrayForEach(this.features(), function(feature) {
            total += feature.price();
        });
    
        return total;
    }, appViewModel);
    
  2. Create your view model in a function:

    var AppViewModel = function() {
        this.features = ko.observableArray([
            new objFeatures("Feature1", 20),
            new objFeatures("Feature2", 20)
        ]);
    
        this.grandTotal = ko.computed(function() {
            var total = 0;
            ko.utils.arrayForEach(this.features(), function(feature) {
                total += feature.price();
            });
            return total;
        }, this);
    };
    
    ko.applyBindings(new AppViewModel());​
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文