Icontem

File: app/components/test-shopping-cart/store/modules/cart.js

Recommend this page to a friend!
  Classes of Sergey Beskorovayniy  >  Vuex Examples  >  app/components/test-shopping-cart/store/modules/cart.js  >  Download  
File: app/components/test-shopping-cart/store/modules/cart.js
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: Vuex Examples
Example apps using Vuex state management pattern
Author: By
Last change: Compatibility wthis IE
Date: 2 years ago
Size: 1,092 bytes
 

Contents

Class file image Download
define([], function () {

    // initial state
    var state = {
        added: [],
        checkoutStatus: null
    };

    var mutations = {
        ADD_TO_CART: function (state, id) {
            var record = _.find(state.added, function(p) { 
                return p.id === id;
            });
            if (!record) {
                state.added.push({
                    id: id,
                    quantity: 1
                });
            } else {
                record.quantity++;
            }
        },
        CHECKOUT_REQUEST: function (state) {
            // clear cart
            state.added = [];
            state.checkoutStatus = null;
        },
        CHECKOUT_SUCCESS: function (state) {
            state.checkoutStatus = 'successful';
        },
        CHECKOUT_FAILURE: function (state, savedCartItems) {
            // rollback to the cart saved before sending the request
            state.added = savedCartItems;
            state.checkoutStatus = 'failed';
        }
    };

    return {
        state: state,
        mutations: mutations
    };

});