My itemCollection
class is designed to store information about purchases. It includes an array called _items
where the purchases are stored. When a user adds a new purchase to their cart, the class utilizes the addItem
method. This method checks if the item is already in the _items
property and either increments the quantity or adds a new item to the array.
The issue I am facing is that instead of adding a new item to the array when a different item is chosen, it simply continues to increment the quantity property of the first item that was added.
cartCollection
class (object):
var cartCollection = {
_items: [],
addItem: function(obj) {
'use strict';
var purchase = {
item: {
id: obj.id,
name: obj.name,
price: obj.price
},
thisItemTotal: obj.price,
quantity: 1
};
var result = _.findWhere(this._items, purchase.item.id);
console.log(result);
if (typeof result != 'undefined') {
//console.log(result);
var index = _.findIndex(this._items, {
id: result.item.id
});
//console.log(index);
result.quantity++;
this._items[index] = result;
this._itemTotalPrice();
} else if (typeof result === 'undefined') {
console.log("I'm being called!");
this._items.push(purchase);
console.log(this._items);
}
},
...