<?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: Saiprasad Devare</title>
    <description>The latest articles on DEV Community by Saiprasad Devare (@saiprasaddevare).</description>
    <link>https://dev.to/saiprasaddevare</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%2F117658%2F79acc1e2-54cf-4d5e-9999-7794923ae910.jpg</url>
      <title>DEV Community: Saiprasad Devare</title>
      <link>https://dev.to/saiprasaddevare</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/saiprasaddevare"/>
    <language>en</language>
    <item>
      <title>I tried Vue.js, Have you...?</title>
      <dc:creator>Saiprasad Devare</dc:creator>
      <pubDate>Fri, 23 Aug 2019 12:51:31 +0000</pubDate>
      <link>https://dev.to/saiprasaddevare/i-tried-vue-js-have-you-4j6e</link>
      <guid>https://dev.to/saiprasaddevare/i-tried-vue-js-have-you-4j6e</guid>
      <description>&lt;h2&gt;
  
  
  Quick Background
&lt;/h2&gt;

&lt;p&gt;Hi all,&lt;br&gt;
This is Saiprasad. I am a full-stack web developer and working in Ruby on Rails, Python, AngularJS.&lt;/p&gt;
&lt;h2&gt;
  
  
  Begin
&lt;/h2&gt;

&lt;p&gt;I've recently started learning the Vue.js. I'm still a novice but thought it would be beneficial to explain how do I used Vue.js knowing AngularJS.&lt;/p&gt;

&lt;p&gt;Two weeks before, I had an issue regarding pagination. And I have to solve the issue with plain Javascript(Since the project does not have any frontend framework). So, by plain Javascript, the issue would get solved, but we tried something new. We decided to use Vue.js.&lt;br&gt;
Since Use-case was only for a simple single component (Pagination) which Vue.js fit's perfect.&lt;/p&gt;

&lt;p&gt;We used the Vue.js standalone mode (There is no need for any webpack build system or vue-cli ).With the backend framework as Django.&lt;/p&gt;

&lt;p&gt;As being from AngularJS background. Was looking toward Vue.js as AngularJS.(This helped me a lot).&lt;/p&gt;
&lt;h2&gt;
  
  
  Vue Lifecycle
&lt;/h2&gt;

&lt;p&gt;The most beautiful thing that Vue.js offers the naming convention. The Lifecycle hooks naming convention are simpler to understand.&lt;/p&gt;

&lt;p&gt;Creation (Initialization)&lt;br&gt;
     &lt;/p&gt;
&lt;ul&gt;

       &lt;li&gt;beforeCreate&lt;/li&gt;

       &lt;li&gt;created&lt;/li&gt;

     &lt;/ul&gt;
&lt;br&gt;
   Mounting (DOM Insertion)&lt;br&gt;
     &lt;ul&gt;

       &lt;li&gt;beforeMount&lt;/li&gt;

       &lt;li&gt;mounted&lt;/li&gt;

     &lt;/ul&gt;
&lt;br&gt;
   Updating (Diff &amp;amp; Re-render)&lt;br&gt;
     &lt;ul&gt;

       &lt;li&gt;beforeUpdate&lt;/li&gt;

       &lt;li&gt;updated&lt;/li&gt;

     &lt;/ul&gt;

&lt;p&gt;Destruction (Teardown)&lt;br&gt;
     &lt;/p&gt;
&lt;ul&gt;

       &lt;li&gt;beforeDestroy&lt;/li&gt;

       &lt;li&gt;destroyed&lt;/li&gt;

     &lt;/ul&gt;

&lt;p&gt;Please check &lt;a href="https://alligator.io/vuejs/component-lifecycle/" rel="noopener noreferrer"&gt;Vue.js Lifecycle&lt;/a&gt; for detailed information of lifecycle hooks. (Helped me a lot)&lt;/p&gt;
&lt;h2&gt;
  
  
  Problems Faced
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Interpolation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We had faced some problem for the Interpolations in Vue.js as I stated we used Django as backend framework.Vue.js has the same syntax as Django templates to denote expressions. So to tackle this we used delimiters as a part of Vue.js&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fo7cy6qfyrq3swgmelc4p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fo7cy6qfyrq3swgmelc4p.png" alt="Creating Delimiter"&gt;&lt;/a&gt;ScreenShot 1.1 Creating the delimiter&lt;/p&gt;

&lt;p&gt;The delimiter is the same as the Ruby language ("#{}".).So to print the value in the HTML of Vue.js use like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F0sfve85bilg1wnve7efc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F0sfve85bilg1wnve7efc.png" alt="Using new Delimiter"&gt;&lt;/a&gt;ScreenShot 1.2 Using the delimiter in HTML&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Array and Object Change Detection&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are Caveats in the detection of Array and Object in Vue.js. As the official document states that "Due to the limitations of modern JavaScript (and the abandonment of Object.observe), Vue cannot detect property addition or deletion."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://vuejs.org/v2/guide/reactivity/#Change-Detection-Caveats" rel="noopener noreferrer"&gt;https://vuejs.org/v2/guide/reactivity/#Change-Detection-Caveats&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So to make object Reactive we should have to declare it upfront. As in &lt;strong&gt;ScreenShot 1.1 Creation of delimiter&lt;/strong&gt;, you can see "pendingUsersInvitation", "pageNo", "pageCount", "recordCount" are declared upfront. Thus these objects are Reactive.&lt;br&gt;
So changing in the objects are updating the UI. You can see below demo example&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F9o4k85zlwgxq2ye5qagg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F9o4k85zlwgxq2ye5qagg.png" alt="sample_example"&gt;&lt;/a&gt;ScreenShot 1.3 Demo Example&lt;/p&gt;

&lt;p&gt;The below screenshot is of adding a new key-value pair to the users object.'Vue instance.object' i.e element.users is accessible in the console and adding a new user to the users, changes the UI.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fpvq5synwhj2ln2oov7az.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fpvq5synwhj2ln2oov7az.png" alt="adding_new_element"&gt;&lt;/a&gt;ScreenShot 1.4 Reactive object in Vue&lt;/p&gt;

&lt;p&gt;This is reactivity in Vue.js.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So how does Vue changes the UI is something interesting. I will explain in short.&lt;/strong&gt;&lt;br&gt;
When we add a plain object to data property to Vue instance. Vue gets all the objects and creates getter and setter method for each object present in data (in the above example for users object).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I] Object.defineProperty&lt;/strong&gt;&lt;br&gt;
    Vue uses Object.defineProperty for setter and getter methods on the objects. The observer pattern is used to update the object's value in Vue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F1lsozf2zedivd73hcz3j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F1lsozf2zedivd73hcz3j.png" alt="define_property"&gt;&lt;/a&gt;ScreenShot 1.5 Adding Getter and Setter to users object&lt;/p&gt;

&lt;p&gt;Don't worry about the terms that are used below. Please check this video,&lt;a href="https://www.vuemastery.com/courses/advanced-components/build-a-reactivity-system" rel="noopener noreferrer"&gt;Reactivity System&lt;/a&gt; explaining the details about the Reactivity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:- If you have watched the above video then below contains will start to make sense.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This getter and setter methods have &lt;strong&gt;depends()&lt;/strong&gt; and &lt;strong&gt;notify()&lt;/strong&gt; methods respectively.&lt;br&gt;
The &lt;strong&gt;depends()&lt;/strong&gt; method push the target to the storage(storage is nothing but an empty array). &lt;br&gt;
The &lt;strong&gt;notify()&lt;/strong&gt; method runs all the target.&lt;br&gt;
When you call an object then the &lt;strong&gt;dep.depends()&lt;/strong&gt; method get called and pushes your function (target).&lt;br&gt;
When you set an object then &lt;strong&gt;dep.notify()&lt;/strong&gt; methods get the call and runs the target. This makes the system reactive.&lt;/p&gt;

&lt;p&gt;So, &lt;strong&gt;the problem that we faced was setting the boolean value to the object key called 'active' wasn't updating the UI.&lt;/strong&gt; The structure of the data is like below,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fhshs3lpr10636bwtticg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fhshs3lpr10636bwtticg.png" alt="data_structure"&gt;&lt;/a&gt;ScreenShot 1.6 Data Structure of the object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: pagination.pendingUsersInvitation is the 'Vue instance.Property' as you can see in screenshot 1.1 declaring Vue instance.&lt;/strong&gt;&lt;br&gt;
This data doesn't have an 'active' key. We are adding explicitly to the data. As follow&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;     &lt;span class="nx"&gt;pagination&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pendingUsersInvitation&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;active&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;True&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But here, Vue doesn't understand that data (pagination.pendingUsersInvitation) has modified and this will not update the changes to the UI.&lt;/p&gt;

&lt;p&gt;Thus, to make Vue understand that data has changed so that Vue can update the UI. We change code to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;     &lt;span class="nx"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pagination&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pendingUsersInvitation&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;active&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This made the changes to the UI as Vue understands that data has been changed.&lt;/p&gt;

&lt;p&gt;In actually we have done something like this,&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fiwitxrmifnc9jlyz51h9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fiwitxrmifnc9jlyz51h9.png" alt="vue_set"&gt;&lt;/a&gt;ScreenShot 1.7 Using Vue.set &lt;/p&gt;

&lt;p&gt;Here, we set the active key by comparing the item.pk == id in the data(pagination.pendingUsersInvitation).So active key get's added to the data if the condition gets satisfied. This change of code had updated the UI.&lt;/p&gt;

&lt;p&gt;finally, 'Interpolation' and 'Array and Object Change Detection' problems were got solved.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pagination Component
&lt;/h3&gt;

&lt;p&gt;The pagination component is ready as below. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fb8yefya43lsmlf6eqd3c.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fb8yefya43lsmlf6eqd3c.jpg" alt="pagination_component"&gt;&lt;/a&gt;ScreenShot 1.8 Pagination component&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Things that were familiar in Vue.js&lt;br&gt;
     &lt;/p&gt;
&lt;ul&gt;

      &lt;li&gt; v-model same as (ng-model in AngularJS) &lt;/li&gt;

      &lt;li&gt; v-if same as (ng-if in AngularJS) &lt;/li&gt;

      &lt;li&gt; v-for same as (ng-repeat in AngularJS) &lt;/li&gt;

      &lt;li&gt; v-bind:class same as (consider as ng-class in AngularJS) &lt;/li&gt;

     &lt;/ul&gt;

&lt;p&gt;As being from AngularJS background, learning Vue.js was fun and easy. Give a try for Vue.js and let us know about how do you feel using Vue.js.&lt;/p&gt;

</description>
      <category>vue</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
