I have built a client side cart, however now i am having an extremely difficult issue, i don't know what to do about it. My cart has a restriction which is you cannot buy tickets from two different vendors or events at the same time. Now if you try to do this what "should" happen is that the cart should clear the previous data in the cart and add the new data.
This is my function that clears my cart:
$cart.clear = function() {
// Clear cart data
$cart.cart = newCart; // Does not work
// Remove cart from storage
delete $localStorage.cart; // Does not work
// Recreate empty cart
$localStorage.cart = $cart.cart; // Does not work
}
The above code works only ONCE that is after the initial page refresh, any attempts to clear the cart after that fail, the data is NEVER deleted.
I have been at this for a while now and now i'm total confused. This is basically what my cart service looks like:
.factory('$cart', function ($window, $localStorage) {
var $cart = {};
// Creating New Cart Object
var newCart = {
info: {},
tickets: [],
customer: {},
extras: []
};
$cart.cart = newCart;
$cart.load = function() {
$cart.cart = $localStorage.cart || newCart;
}
$cart.update = function() {
$localStorage.cart = $cart.cart;
}
$cart.clear = function() {
...
}
$cart.addTicket = function(params) {
...
};
$cart.removeTicket = function(index) {
...
}
$cart.removeExtra = function(index) {
...
}
var init = function() {
$cart.load();
}
init();
return $cart;
})
I was using $window.localStorage.setItem()
and $window.localStorage.removeItem
before to add and remove items from my cart, when i noticed this issue i switched to ngStorage to see if that would work but it still did not work.