0

this is my itemModel:

var itemModel = function (id,description,picture,totalInventory,retailPrice,dealerPrice,category,brand,unitOfMeasure,createdBy,status,highestCost, lowestCost) {
var self = this;

self.ID = ko.observable(id);
self.Description = ko.observable(description);
self.Picture = ko.observable(picture);
self.TotalInventory = ko.observable(totalInventory);
self.RetailPrice = ko.observable(retailPrice);
self.DealerPrice = ko.observable(dealerPrice);
self.Category = ko.observable(category);
self.Brand = ko.observable(brand);
self.UnitOfMeasure = ko.observable(unitOfMeasure);
self.CreatedBy = ko.observable(createdBy);
self.Status = ko.observable(status);
self.HighestCost = ko.observable(highestCost);
self.LowestCost = ko.observable(lowestCost);};

I have this method to get the items

self.GetItems = function(){
   $.getJSON("/Items/GetItems",{status: self.ItemStatus()}, function (result) {
      for (var i = 0, j = result.data.length; i < j; i++){
            item = result.data[i];
            underlyingArray.push(new itemModel(
                item.ID,
                item.Description,
                item.Picture,
                ....computetotalinventoryhere...,
                item.RetailPrice,
                item.DelearPrice,
                item.Category,
                item.Brand,
                item.UnitOfMeasure,
                item.CreatedBy,
                item.Status,
                item.HighestCost,
                item.LowestCost
            ));
        }
        self.list.valueHasMutated();
   }

};

I want to create a function to compute the total inventory. Is that possible in knockout? or any suggestion on how to do this. thanks.

4
  • Where does underlyingArray variable come from? Are your missing an assignment before the for loop ? Is the total across ALL items in "self.list" or does each itemModel have its own total value Commented Mar 4, 2014 at 2:09
  • above that i set the variables var item, underlyingArray = self.list(); Commented Mar 4, 2014 at 3:28
  • What about the other questions... it could influence the answer Commented Mar 4, 2014 at 3:48
  • I edited the script above. I added the itemModel. Commented Mar 4, 2014 at 10:29

1 Answer 1

2

Use a computed observable

var totalInv = ko.computed(function(){
   var total = 0;
   for(var i = 0; i < underlyingArray().length; i++){
       total += underlyingArray()[i].RetailPrice; // OR OTHER VALUES AS NEEDED
   }

   return total;  // or other values as needed
});
Sign up to request clarification or add additional context in comments.

1 Comment

If you have to luxury of ES5 I suggest the use of map/reduce instead of for loop with external total variable "return underlyingArray().map(~selectorFn~).reduce( function(prev, curr) { return prev + curr; }, 0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.