Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ember-cart

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ember-cart

Shopping cart primitives for Ember applications

  • 0.1.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

ember-cart

Build Status

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.

DockYard, Inc © 2015

@dockyard

Licensed under the MIT license

Keywords

FAQs

Package last updated on 22 Jun 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc