local-cart
v0.0.3
Published
* **v0.0.1, unstable,** Simple api to store data for shopping carts, wishlists in browser localstorage
Downloads
2
Readme
Local cart
- v0.0.1, unstable, Simple api to store data for shopping carts, wishlists in browser localstorage
Features
- Local storage with basil.js
- Cart object is mixed with node native event emmiter
Install
npm install stampit
Instantiate
require('local-cart'); //add cart factory to window object
const myCart = window.cartFactory({
name: 'myAwesomeShopCart'
})
options
name
name is used to store data in local storage, to restore cart with previuosly saved data
Methods
addItem(opts)
params
id
- item identificator in cartcount
- number of items in cart, if not specified is1
unit_price
- price of 1 item in cartdata
- this is place to store your product model data, api doesn't use it,
var myProductModel = {
id: 1,
price:1000,
title: 'Interesting Book',
author: 'Jogn Doe'
}
var newCartItemOptions = {
id: myProductModel.id,
unit_price: myProductModel.price,
data: myProductModel
}
myCart.addItem(newCartItemOptions);
Adding product with id
that already exists in cart only updates count
of an item
myCart.addItem(newCartItemOptions);
console.log(myCart.items); // [Object]
console.log(myCart.getTotalCount); // 1
// add same item again
myCart.addItem(newCartItemOptions);
console.log(myCart.items); // [Object]
console.log(myCart.getTotalCount); // 2
// let's change count property
newCartItemOptions.count = 8;
myCart.addItem(newCartItemOptions);
console.log(myCart.items); // [Object]
console.log(myCart.getTotalCount); // 10
getItem(id)
Returns item with given id
updateItemCount(id, count)
Update count
of existing item in cart
deleteItem(id)
Delete item from cart
getTotalPrice()
Total price of all items.
getTotalCount()
Total count of all items.
Events
Cart object is also an event emmiter. By now it emits
only one event. See node event emmiter api
change
- fires when anything in cart is changed, afteraddItem
,deleteItem
,updateItemCount
methods
myCart.on('change', functon () {
console.log('items changed');
});