Skip to content

Instantly share code, notes, and snippets.

@gajus
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gajus/c3489e0f596159a1b352 to your computer and use it in GitHub Desktop.
Save gajus/c3489e0f596159a1b352 to your computer and use it in GitHub Desktop.
angular
.module('cart', []);
.factory('cart', function () {
var cartData = {};
return {
addProduct: function (id, name, price) {
var addToExistingItem = false;
for (var i = 0; i < cartData.length; i++) {
if (cartData[i].id == id) {
cartData[i].count++;
addedToExistingItem = true;
break;
}
}
if (!addedToExistingItem) {
cartData.push({
count: 1, id: id, price: price, name: name
});
}
},
removeProduct: function (id) {
for (var i = 0; i < cartData.length; i++) {
if (cartData[i].id == id) {
cartData.splice(i, 1);
break;
}
}
},
getProducts: function () {
return cartData;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment