Complete Vue.js 2 Web Development_ Practical guide to building end-to-end web development solutions with Vue.js 2

(singke) #1
Building an E-Commerce Store - Browsing Products Chapter 10

If it does not, add a new object with the name, handle, and a count, which is an array of


product handles. We store an array of handles so that we can verify that the product has


already been seen. If we were keeping a raw numerical count, we could encounter a


scenario where the filters get triggered twice, doubling the count. By checking whether the
product handle exists already, we can check it's only been seen once.


If a filter of that name does exist, add the handle to the array after checking it does not exist:


filters() {
if(this.categoriesExist) {
let category = this.categoryProducts(this.slug),
vendors = this.topics.vendor;

for(let product of category.productDetails) {

if(product.hasOwnProperty('vendor')) {
let vendor = product.vendor;

if(vendor.handle) {
if(!vendor.handle.count.includes(product.handle)) {
category.values[item.handle].count.push(product.handle);
}
} else {
vendors.values[vendor.handle] = {
...vendor,
count: [product.handle]
} } } } } } }

This utilizes the previously-used object-expanding ellipsis (...), which saves us from


having to write:


vendors.values[product.vendor.handle] = {
title: vendor.title,
handle: vendor.handle,
count: [product.handle]
}
Free download pdf