kickout.js:在observableArray上没有concat [英] knockout.js: No concat on observableArray

查看:54
本文介绍了kickout.js:在observableArray上没有concat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅从基因敲除(snockout.js)开始,但在尝试基于2个不同的observableArray s制作computed方法时已经遇到了一些麻烦

Only starting with knockout.js, but already running into some trouble when trying to make a computed method based on 2 different observableArrays

使用基因敲除网站上的文档,我创建了以下视图模型:

Using the documentation on knockout.js' website, I've created the following viewmodel:

var Cart = function() {
  var self = this;

  self.Products = ko.observableArray([]);
  self.Products2 = ko.observableArray([]);
  self.Messages = ko.observableArray([]);

  self.TotalAmount = ko.computed(function() {
    var result = 0;
    ko.utils.arrayForEach(
      this.Products().concat(this.Products2()),
      function(item) {
        result+=item.AmountIncludingVAT();
      }
    );
    return result;
  });
};

这样做会引发错误"Uncaught TypeError: Object #<error> has no method 'concat'.

我知道有一个叫做arrayPushAll的函数,但这是一个破坏性的函数,它将改变原始的observableArray. (我不认为这是我想要的东西.)

I know there is this function called arrayPushAll, but it's a destructive function which would alter the original observableArray. (I don't think this is something I want).

是否有任何 clean 方法可以实现我要完成的任务?还是我必须对arrayForEach进行2次不同的调用,每个数组一次?

Is there any clean way to achieve what I'm trying to do? Or do I have to make 2 different calls to arrayForEach, one for each array?

推荐答案

更改:

this.Products().concat(this.Products2()),

收件人:

self.Products().concat(self.Products2()),

在TotalAmount ko.computing函数内部.

Inside your TotalAmount ko.computed function.

this在您计算的上下文中是指全局对象而不是视图模型.因此,您需要使用为self变量分配的正确的this值.

this in the context of your computed refers to the global object rather than the view model. So you need to use the self variable that is assigned the correct this value earlier.

工作示例- http://jsfiddle.net/55kZp/

这篇关于kickout.js:在observableArray上没有concat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆