class ArtboxBasket { private _items; get items() { return this._items; } constructor() { $.get('/basket', function (data) { this._items = data.basket; this.updateModal(data.modal); }.bind(this), 'json').fail(function() { console.error('Basket cannot be init'); }); } public add(product_variant_id, count) { $.post('/basket/add?product_variant_id='+product_variant_id+'&count='+count, function (data) { this._items = data.basket; this.updateModal(data.modal, true); }.bind(this), 'json').fail(function (xhr, status, error) { console.error(error); }); } public set(product_variant_id, count) { $.post('/basket/set?product_variant_id='+product_variant_id+'&count='+count, function (data) { this._items = data.basket; this.updateModal(data.modal); }.bind(this), 'json').fail(function (xhr, status, error) { console.error(error); }); } public updateModal(modal, show = false) { var modalBox = $('#buyForm'); modalBox.html(modal); if(this.count < 1) { modalBox.modal('hide'); } else if(show) { modalBox.modal('show'); } this.updateCart(); } public updateCart() { var cart = $('#top-cart-content'); var count = this.count; if(count > 0) { $(cart).html('
В корзине' + count + ' товар' + ((count > 4) ? 'ов' : ((count > 1) ? 'а' : '')) + '
на ' + this.sum + '
оформить покупку
'); } else { $(cart).html('

Корзина пуста

'); } } get count(): number { return Object.keys(this._items).length; } get sum(): number { var sum = 0; $.each(this._items, function(index, value) { sum += value.price * value.count; }); return sum; } }