<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jumzeey</title>
    <description>The latest articles on DEV Community by Jumzeey (@jumzeey).</description>
    <link>https://dev.to/jumzeey</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F400365%2F25c38ece-a3a2-4648-a6e7-dfb024d622a3.jpeg</url>
      <title>DEV Community: Jumzeey</title>
      <link>https://dev.to/jumzeey</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jumzeey"/>
    <language>en</language>
    <item>
      <title>5 Real life use cases of VueJs Computed properties, with code examples.</title>
      <dc:creator>Jumzeey</dc:creator>
      <pubDate>Wed, 31 May 2023 15:39:29 +0000</pubDate>
      <link>https://dev.to/jumzeey/10-real-life-use-cases-of-vuejs-computed-properties-with-code-examples-4e0p</link>
      <guid>https://dev.to/jumzeey/10-real-life-use-cases-of-vuejs-computed-properties-with-code-examples-4e0p</guid>
      <description>&lt;p&gt;In Vue.js, computed properties are functions that are used to calculate and return a value based on the current state of the Vue instance. They are defined in the computed property of a Vue component and can be accessed like data properties within the template or other computed properties.&lt;/p&gt;

&lt;p&gt;Computed properties are useful when you need to perform some logic or calculations on the data and generate a derived value that can be used in the template. The computed property will be updated automatically whenever the data it depends on changes, ensuring that the value is always up to date.&lt;/p&gt;

&lt;p&gt;Here's an example of how to define computed properties in Vue.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data() {
    return {
      firstName: 'John',
      lastName: 'Doe'
    };
  },
  computed: {
    fullName() {
      return this.firstName + ' ' + this.lastName;
    },
    reversedFullName() {
      return this.fullName.split('').reverse().join('');
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, we have a component with two data properties firstName and lastName. We define two computed properties fullName and reversedFullName. The fullName computed property concatenates the first and last name, while the reversedFullName computed property reverses the characters of the fullName property.&lt;/p&gt;

&lt;p&gt;In the template, we can then use these computed properties like regular data properties:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
  &amp;lt;div&amp;gt;
    &amp;lt;p&amp;gt;Full Name: {{ fullName }}&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;Reversed Full Name: {{ reversedFullName }}&amp;lt;/p&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whenever the firstName or lastName data properties change, the fullName and reversedFullName computed properties will be re-evaluated and the template will be updated accordingly.&lt;/p&gt;

&lt;p&gt;Computed properties provide a clean and declarative way to work with derived values in Vue.js and can help keep your code more readable and maintainable.&lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  5 MORE REAL CASE EXAMPLES OF COMPUTED PROPERTIES.
&lt;/h2&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Shopping Cart&lt;/strong&gt;: Calculate the total price or quantity of items in a shopping cart based on the individual item prices and quantities.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
  &amp;lt;div&amp;gt;
    &amp;lt;ul&amp;gt;
      &amp;lt;li v-for="item in cartItems" :key="item.id"&amp;gt;
        {{ item.name }} - {{ item.price }} - Quantity: {{ item.quantity }}
      &amp;lt;/li&amp;gt;
    &amp;lt;/ul&amp;gt;
    &amp;lt;p&amp;gt;Total Price: {{ totalPrice }}&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;Total Quantity: {{ totalQuantity }}&amp;lt;/p&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;

&amp;lt;script&amp;gt;
export default {
  data() {
    return {
      cartItems: [
        { id: 1, name: 'Item 1', price: 10, quantity: 2 },
        { id: 2, name: 'Item 2', price: 15, quantity: 1 },
        { id: 3, name: 'Item 3', price: 20, quantity: 3 }
      ]
    };
  },
  computed: {
    totalPrice() {
      return this.cartItems.reduce((total, item) =&amp;gt; total + item.price * item.quantity, 0);
    },
    totalQuantity() {
      return this.cartItems.reduce((total, item) =&amp;gt; total + item.quantity, 0);
    }
  }
};
&amp;lt;/script&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we have a cartItems array that represents items in a shopping cart. The computed properties totalPrice and totalQuantity calculate the total price and total quantity of items in the cart, respectively, using the reduce() method.&lt;/p&gt;

&lt;p&gt;The reduce() method is used with an initial value of 0 for both computed properties. It iterates over the cartItems array and accumulates the total price and quantity by multiplying the price with the quantity for each item.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Form Validation:&lt;/strong&gt; Check if form fields meet certain validation criteria, such as required fields, maximum length, or specific patterns.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
  &amp;lt;form @submit="submitForm"&amp;gt;
    &amp;lt;input v-model="name" :class="{ 'is-invalid': !isNameValid }" type="text" placeholder="Name" required&amp;gt;
    &amp;lt;input v-model="email" :class="{ 'is-invalid': !isEmailValid }" type="email" placeholder="Email" required&amp;gt;
    &amp;lt;button type="submit" :disabled="!isFormValid"&amp;gt;Submit&amp;lt;/button&amp;gt;
  &amp;lt;/form&amp;gt;
&amp;lt;/template&amp;gt;

&amp;lt;script&amp;gt;
export default {
  data() {
    return {
      name: '',
      email: ''
    };
  },
  computed: {
    isNameValid() {
      return this.name.length &amp;gt; 0;
    },
    isEmailValid() {
      return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(this.email);
    },
    isFormValid() {
      return this.isNameValid &amp;amp;&amp;amp; this.isEmailValid;
    }
  },
  methods: {
    submitForm() {
      // Submit form logic
    }
  }
};
&amp;lt;/script&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this example, we have a form with name and email fields. The computed properties isNameValid, isEmailValid, and isFormValid are used for form validation.&lt;/p&gt;

&lt;p&gt;The isNameValid computed property checks if the name field has a non-empty value. The isEmailValid computed property uses a regular expression to check if the email field has a valid email format. The isFormValid computed property checks if both the name and email fields are valid.&lt;/p&gt;

&lt;p&gt;The CSS class is-invalid is conditionally applied to the input fields based on their validation status.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Filtering and Sorting:&lt;/strong&gt; Compute a filtered or sorted version of a data list based on user-defined criteria, such as filtering products by category or sorting a list of tasks by priority.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
  &amp;lt;div&amp;gt;
    &amp;lt;select v-model="selectedCategory"&amp;gt;
      &amp;lt;option value=""&amp;gt;All&amp;lt;/option&amp;gt;
      &amp;lt;option v-for="category in categories" :key="category.id" :value="category.id"&amp;gt;{{ category.name }}&amp;lt;/option&amp;gt;
    &amp;lt;/select&amp;gt;

    &amp;lt;ul&amp;gt;
      &amp;lt;li v-for="product in filteredProducts" :key="product.id"&amp;gt;
        {{ product.name }} - {{ product.price }}
      &amp;lt;/li&amp;gt;
    &amp;lt;/ul&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;

&amp;lt;script&amp;gt;
export default {
  data() {
    return {
      selectedCategory: '',
      categories: [
        { id: 1, name: 'Category 1' },
        { id: 2, name: 'Category 2' },
        { id: 3, name: 'Category 3' }
      ],
      products: [
        { id: 1, name: 'Product 1', price: 10, categoryId: 1 },
        { id: 2, name: 'Product 2', price: 15, categoryId: 2 },
        { id: 3, name: 'Product 3', price: 20, categoryId: 3 }
      ]
    };
  },
  computed: {
    filteredProducts() {
      if (this.selectedCategory) {
        return this.products.filter(product =&amp;gt; product.categoryId === this.selectedCategory);
      } else {
        return this.products;
      }
    }
  }
};
&amp;lt;/script&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we have a list of products with name, price, and categoryId properties. The computed property filteredProducts is used to filter the products based on the selectedCategory value.&lt;/p&gt;

&lt;p&gt;The  element allows the user to select a category, and the v-model directive binds the selected category to the selectedCategory data property.&lt;/p&gt;

&lt;p&gt;The filteredProducts computed property filters the products array based on the selected category. If a category is selected, only products with a matching categoryId are returned. If no category is selected, all products are returned.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Pagination:&lt;/strong&gt; Determine the current page and number of pages based on the total number of items and items per page, enabling easy pagination navigation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
  &amp;lt;div&amp;gt;
    &amp;lt;ul&amp;gt;
      &amp;lt;li v-for="item in paginatedItems" :key="item.id"&amp;gt;
        {{ item.name }} - {{ item.price }}
      &amp;lt;/li&amp;gt;
    &amp;lt;/ul&amp;gt;

    &amp;lt;div&amp;gt;
      &amp;lt;button @click="previousPage" :disabled="currentPage === 1"&amp;gt;Previous&amp;lt;/button&amp;gt;
      &amp;lt;span&amp;gt;Page {{ currentPage }} of {{ totalPages }}&amp;lt;/span&amp;gt;
      &amp;lt;button @click="nextPage" :disabled="currentPage === totalPages"&amp;gt;Next&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;

&amp;lt;script&amp;gt;
export default {
  data() {
    return {
      currentPage: 1,
      itemsPerPage: 3,
      items: [
        { id: 1, name: 'Item 1', price: 10 },
        { id: 2, name: 'Item 2', price: 15 },
        { id: 3, name: 'Item 3', price: 20 },
        { id: 4, name: 'Item 4', price: 25 },
        { id: 5, name: 'Item 5', price: 30 },
        { id: 6, name: 'Item 6', price: 35 },
        { id: 7, name: 'Item 7', price: 40 }
      ]
    };
  },
  computed: {
    paginatedItems() {
      const startIndex = (this.currentPage - 1) * this.itemsPerPage;
      const endIndex = startIndex + this.itemsPerPage;
      return this.items.slice(startIndex, endIndex);
    },
    totalPages() {
      return Math.ceil(this.items.length / this.itemsPerPage);
    }
  },
  methods: {
    previousPage() {
      if (this.currentPage &amp;gt; 1) {
        this.currentPage--;
      }
    },
    nextPage() {
      if (this.currentPage &amp;lt; this.totalPages) {
        this.currentPage++;
      }
    }
  }
};
&amp;lt;/script&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we have a list of items with name and price properties. The computed properties paginatedItems and totalPages are used for pagination.&lt;/p&gt;

&lt;p&gt;The paginatedItems computed property calculates the start and end indices based on the current page and items per page. It uses the slice() method to return a portion of the items array corresponding to the current page.&lt;/p&gt;

&lt;p&gt;The totalPages computed property calculates the total number of pages by dividing the total number of items by the items per page and rounding up using Math.ceil().&lt;/p&gt;

&lt;p&gt;The previous and next page buttons update the currentPage value, which triggers the re-computation of the paginatedItems computed property and enables navigation between pages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Conditional Styling:&lt;/strong&gt; Dynamically apply CSS classes or styles to elements based on certain conditions, like highlighting overdue tasks or applying different styles to completed items.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
  &amp;lt;div&amp;gt;
    &amp;lt;ul&amp;gt;
      &amp;lt;li v-for="task in tasks" :key="task.id" :class="{ 'task-overdue': isOverdue(task), 'task-completed': task.completed }"&amp;gt;
        {{ task.name }}
      &amp;lt;/li&amp;gt;
    &amp;lt;/ul&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;

&amp;lt;script&amp;gt;
export default {
  data() {
    return {
      tasks: [
        { id: 1, name: 'Task 1', dueDate: '2023-06-01', completed: false },
        { id: 2, name: 'Task 2', dueDate: '2023-05-30', completed: true },
        { id: 3, name: 'Task 3', dueDate: '2023-06-02', completed: false }
      ]
    };
  },
  methods: {
    isOverdue(task) {
      const dueDate = new Date(task.dueDate);
      const today = new Date();
      return dueDate &amp;lt; today;
    }
  }
};
&amp;lt;/script&amp;gt;

&amp;lt;style scoped&amp;gt;
.task-overdue {
  color: red;
  font-weight: bold;
}

.task-completed {
  text-decoration: line-through;
}
&amp;lt;/style&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we have a list of tasks with name, dueDate, and completed properties. The isOverdue method checks if a task is overdue by comparing the due date with the current date.&lt;/p&gt;

&lt;p&gt;The v-for directive iterates over the tasks array and applies conditional CSS classes using the :class binding. The task-overdue class is applied to tasks that are overdue, and the task-completed class is applied to completed tasks.&lt;/p&gt;

&lt;p&gt;The scoped  block defines the styles for the conditional classes. Overdue tasks are displayed in red with bold font weight, and completed tasks have a line-through text decoration.&amp;lt;/p&amp;gt;

&amp;lt;blockquote&amp;gt;
&amp;lt;p&amp;gt;These examples showcase the practical use of computed properties in various real-life scenarios. Feel free to use them in your projects.&amp;lt;/p&amp;gt;
&amp;lt;/blockquote&amp;gt;
&lt;/p&gt;

</description>
      <category>vue</category>
      <category>javascript</category>
      <category>frontend</category>
      <category>webdev</category>
    </item>
    <item>
      <title>My first laravel + vue App</title>
      <dc:creator>Jumzeey</dc:creator>
      <pubDate>Fri, 03 Jul 2020 19:41:24 +0000</pubDate>
      <link>https://dev.to/jumzeey/my-first-laravel-vue-app-390a</link>
      <guid>https://dev.to/jumzeey/my-first-laravel-vue-app-390a</guid>
      <description>&lt;p&gt;If you are a beginner in development and you are yet to build your first project, what are you waiting for? give it a shot, even if it has to do with following up a tutorial project, add some spice to it along the way. This project was inspired by andre Madarang youtube channel (&lt;a href="https://www.youtube.com/playlist?list=PLEhEHUEU3x5pYTjZze3fhYMB4Nl_WOHI4"&gt;https://www.youtube.com/playlist?list=PLEhEHUEU3x5pYTjZze3fhYMB4Nl_WOHI4&lt;/a&gt;). The fact that it was a project made by following his youtube tutorial, doesn't mean it was easy. There was quite a boatload of hiccups and obstacles along the way, which almost made me give up on the project. But, I really wanted to complete this project of mine. I really wanted to assign such accomplishment to myself, all of which kept driving me to continue.it began with a mindset of wanting to learn laravel PHP framework, I downloaded several books, but they weren't giving me the push I needed, then I decided it's best I challenge myself big time. Then, my next step was to go to youtube, on searching for laravel, brad traversy media's laravel course came up (&lt;a href="https://www.youtube.com/playlist?list=PLillGF-RfqbYhQsN5WMXy6VsDMKGadrJ-"&gt;https://www.youtube.com/playlist?list=PLillGF-RfqbYhQsN5WMXy6VsDMKGadrJ-&lt;/a&gt;). I followed up his tutorial, and I can tell you, he was really good, but deep down inside I needed something much more advanced, something that pose to become a real challenge. On the quest for this, andre course showed up. At first, the course is based on building aa movie app, using TMDB's Api and laravel 7 HTTP client, this alone was enough to spike my interest. So, I was like well..... Let's get to it. We started out by first installing all the needed dependencies, compiling the needed assets via webpack and then building out the UI using Tailwind CSS. This is the first of hearing about tailwind CSS, I wasn't ready to learn a new CSS framework, so I decided to follow up using my knowledge of bootstrap, to cut the long story short, I just decided to give Tailwind a try and it turned out to be great. Along the way, some of the obstacles I faced was integrating laravel livewire (for the play trailer button) and alphine js(for the search component). Eventually, I decided to use my very own vue js to write out the search component, using a step by step guide from (Christain Nicholas - create a searchable dropdownlist-medium)article, which also increased my problems.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;vue js doesn't use blade routing.&lt;/li&gt;
&lt;li&gt;I had to learn Axios, in order to call the search API endpoints into laravel.
&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkr832pyr04nyv5edrwmv.png" alt="Alt Text" width="800" height="515"&gt;
To fix the issue of routing in vuejs, vue router wasn't an option for I wasn't building a SPA, so I had to parse the link manually.
All in all, it was a really good experience, tackling problems and debugging the app, really gave me a sense of purpose, of wanting to become a developer. Do check out the app at &lt;a href="https://zonamv1.herokuapp.com/"&gt;https://zonamv1.herokuapp.com/&lt;/a&gt;. I plan to release more features to it in the nearest future. Explore, but please don't break.
if you ever want to learn a new framework, try watching a project being built out of it, to begin with, then venture into whichever method works best for you.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>webdesign</category>
      <category>laravel</category>
      <category>vue</category>
      <category>tailwindcss</category>
    </item>
  </channel>
</rss>
