javascript - knockout.js: No concat on observableArray -


only starting knockout.js, running trouble when trying make computed method based on 2 different observablearrays

using documentation on knockout.js' website, i've created 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;   }); }; 

doing throws error of "uncaught typeerror: object #<error> has no method 'concat'.

i know there function called arraypushall, it's destructive function alter original observablearray. (i don't think want).

is there clean way achieve i'm trying do? or have make 2 different calls arrayforeach, 1 each array?

change:

this.products().concat(this.products2()), 

to:

self.products().concat(self.products2()), 

inside totalamount ko.computed function.

this in context of computed refers global object rather view model. need use self variable assigned correct this value earlier.

working example - http://jsfiddle.net/55kzp/


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -