ember-cart
v0.1.0
Published
Shopping cart primitives for Ember applications
Downloads
3
Readme
ember-cart
About
Shopping cart primitives for Ember applications
Installing
ember install ember-cart
Looking for help?
If it is a bug please open an issue on GitHub.
Usage
ember-cart manages the adding, quantity editing, removal, and eventual payment processing of shopping cart items.
The primitives provided are intended to give a boilerplate base to work from. Customizing the templates is always a good place to start.
Components
{{cart-items}}
Provides a list of the shopping cart. Each line item is a {{cart-item}}. Shows the total cost of all the items in the cart.
{{cart-item}}
The line-item for each item type in the cart. Adding more than one of the same type will increase the quantity. Also handles removal of the item from the cart.
{{cart-counter}}
A counter that displays the current number of unique items in the cart.
this.cart
this.cart
is injected into Controllers and Components. If you want to
add an item to the cart you can create an action that does
this.cart.pushItem
:
actions: {
pushItem(item) {
this.cart.pushItem(item);
}
}
Cart Items
You can push POJOs or Ember models into the cart. The basic information
that an item
requires is name and cost. ember-cart will handle
checking to see if there is already an existing similar item in the
cart. If there is, the quantity for that item is incremented. If not the
item is added to the cart.
POJOs
POJOs pushed into the cart:
this.cart.pushItem({
name: 'Doggie',
cost: 400
});
Ember Models
You can push another model into the cart. However you should provide a
toCartItem
handler on that model to typecast that model to a CartItem.
// app/models/dog.js
export DS.Model.extend({
toCartItem() {
let CartItem = this.container.lookupFactory('model:cart-item');
return CartItem.create({
name: get(this, 'name'),
cost: get(this, 'price')
});
}
});
Persisting to localStorage
If you want to persist the cart to localStorage
so the items are
available if the user comes back later you will need to set the
localStorage
flag in the Cart service to true
:
// app/services/cart.js
import CartService from 'ember-cart/services/cart';
export default CartService.extend({
localStorage: true
});
Please note: if you persist to localStorage
you will have to handle
the security around this. For example, if a user signs out of their
account you will probably want to clear their cart:
actions: {
signOut() {
// signout handler
this.cart.clearItems();
}
}
Make sure to use clearItems()
and not clear()
as the former will
ensure that localStorage
is properly cleared out.
Avoiding quantity incrementation
You may only allow one of a specific type. To enforce the quantity
doesn't increment you should set increment: false
for the Cart Item:
this.cart.pushItem({
name: 'Foo',
price: 100,
increment: false
});
Authors
We are very thankful for the many contributors
Versioning
This library follows Semantic Versioning
Want to help?
Please do! We are always looking to improve this library. Please see our Contribution Guidelines on how to properly submit issues and pull requests.
Legal
DockYard, Inc © 2015