<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Venky on Medium]]></title>
        <description><![CDATA[Stories by Venky on Medium]]></description>
        <link>https://medium.com/@yknevenky?source=rss-b30af7fe1d58------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*y8GSft7UJYLZqjBb-tYbsA.jpeg</url>
            <title>Stories by Venky on Medium</title>
            <link>https://medium.com/@yknevenky?source=rss-b30af7fe1d58------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 13:30:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yknevenky/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Homebrew and it’s quirky naming system]]></title>
            <link>https://medium.com/@yknevenky/homebrew-and-its-quirky-naming-system-2fb48e6c258b?source=rss-b30af7fe1d58------2</link>
            <guid isPermaLink="false">https://medium.com/p/2fb48e6c258b</guid>
            <category><![CDATA[homebrew]]></category>
            <category><![CDATA[macos]]></category>
            <dc:creator><![CDATA[Venky]]></dc:creator>
            <pubDate>Sat, 11 Jan 2025 15:27:27 GMT</pubDate>
            <atom:updated>2025-01-11T15:31:20.171Z</atom:updated>
            <content:encoded><![CDATA[<p>Decoding the Brewing terms of Homebrew</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2TiI9-2_U1r3Ee0HlhFOFQ.png" /></figure><p>I’ve been using Homebrew a lot — just copy-pasting commands and running away from actually understanding what I was dealing with. Suddenly, for some reason, I became curious and wanted to learn more about Homebrew. I started by asking ChatGPT, and I found something funny. I started feeling a bit dumb because I didn’t know the terms and language used in Homebrew. I don’t know why, but I felt that way. I wanted to write about it, so here I am, sharing my thoughts with you.</p><p><strong>Formula</strong> — Basically the script used to install a package.</p><p><strong>Cask</strong> — Used to install MacOS GUI apps</p><p><strong>Prefix</strong> — The path where Homebrew is installed.</p><p><strong>Keg</strong> — The installation destination directory, like an actual keg that holds the installed package.</p><p><strong>Rack</strong> — A directory that holds a collection of kegs (versioned kegs).</p><p><strong>Keg-only </strong>— A keg is considered “keg-only” if it’s not symlinked to the prefix.</p><p><strong>Cellar</strong> — The directory containing one or more racks.</p><p><strong>Caskroom</strong> —The directory containing one or more casks.</p><p><strong>Tap</strong> — The repository from which you fetch a formula.</p><p><strong>Bottle</strong> — pre-built <strong>keg</strong> poured into a <strong>rack</strong> of the <strong>Cellar</strong> instead of building from upstream sources (Yes, I copy-pasted this from Homebrew — hang on, I will explain).</p><blockquote>So <strong>formula</strong> is like the beer, that you fetch from a <strong>tap</strong>, and pour it into a <strong>keg</strong>, and you <strong>rack</strong> a collection of kegs into a <strong>Cellar</strong>.</blockquote><p>And a <strong>Bottle</strong> is a precompiled version of a package (a <strong>Keg</strong>) that has been built by someone else (usually the Homebrew maintainers). This Bottle is poured (aka installed) directly into the Cellar, skipping the need to build the package from source. It’s a <strong>pre-built binary</strong> version of the Keg, making installation much faster.</p><p>It’s amusing how they drew parallels with the brewing process and named the package management system after it. I’d love to hear your thoughts and have a discussion about it!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2fb48e6c258b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to Directives | Learn Angular]]></title>
            <link>https://medium.com/@yknevenky/introduction-to-directives-learn-angular-54e5f94de350?source=rss-b30af7fe1d58------2</link>
            <guid isPermaLink="false">https://medium.com/p/54e5f94de350</guid>
            <category><![CDATA[attributes]]></category>
            <category><![CDATA[structural]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[directives]]></category>
            <category><![CDATA[learn-angular]]></category>
            <dc:creator><![CDATA[Venky]]></dc:creator>
            <pubDate>Tue, 01 Sep 2020 06:00:19 GMT</pubDate>
            <atom:updated>2020-09-02T05:07:56.799Z</atom:updated>
            <content:encoded><![CDATA[<p>Digging deep into built-in Directives in Angular</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ps9rauPh3yE_4MZrbsjugg.png" /><figcaption>Haha, just for fun</figcaption></figure><blockquote><strong>Note:</strong><br>This is one of the several articles related to <strong>“LEARN ANGULAR” series</strong>, that I create. So if you don’t understand something in this article, kindly have a look at the previous articles.</blockquote><p>Before starting with the Directives, please know that, I make use of the <strong>demo-app</strong> project as we did in the Bindings in action article but with some changes. As for now, the <strong>app.component.ts</strong> looks like this, the <strong>app.component.css</strong> and <strong>app.component.html</strong> are empty.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/697/1*iaJOvU2dBQVSeSWr7OzaGA.png" /><figcaption>app.component.ts</figcaption></figure><h3>What are directives ?</h3><blockquote>Directives are used to extend extend the power of the HTML by giving it new syntax.</blockquote><p>If you have used <strong>ngClass</strong>, <strong>ngStyle</strong> and <strong>ngModel</strong>, well they are the directives. You can see that, from <strong>ngClass</strong>, <strong>ngStyle</strong> and <strong>ngModel</strong> does help you to add power to <strong>HTML</strong> with less code. That’s the use case of Directives.</p><p>You can also say, it’s a function that gets executed whenever Angular finds it in the DOM and makes the changes in the target element.</p><p>If you want to add some logic/functionality dealing with the HTML manipulation, that you often find yourself using many times, you can seek the help of directives to eliminate some boilerplate code. It’s a way of code reuse.</p><p>But those are directives that you need to make. We are not going to make any custom Directives in this article, since that requires bit more knowledge than we have now, we will see only the built-in directives in this article. After all the title of course is Introduction to directives.</p><p>Let’s look at the types of directives and some built in directives that Angular provides.</p><h3>Types of directives</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hHR2HYh75ikhDouFbWdViw.png" /><figcaption>Types of directives</figcaption></figure><p>There are three kinds of directives in Angular:</p><ol><li><strong>Components</strong> — directives with a template.</li><li><strong>Structural directives</strong> — change the <strong>DOM</strong> layout by adding and removing <strong>DOM</strong> elements.</li><li><strong>Attribute directives </strong>— change the appearance or behavior of an element, component, or another directive.</li></ol><h3><strong>Component directive</strong></h3><p>Components are actually nothing but a <strong>Directive with a template</strong>, the other two kinds don’t have a template to deal with. And it makes sense right? to think that <strong>Component</strong> as a Directive, when it’s nothing but a thing that helps us to deal with HTML better.</p><p>Under the hood, component deals with the Directive API abstracting away the details from us, to give us a cleaner and easier way to code. We will discuss about this part once we have enough understanding, which sadly needs a lot of more clarity than we have. I will surely cover this in the end of the series, no worries.</p><h3><strong>Attribute directive</strong></h3><p>They helps us to change the behavior or appearance of the DOM element.</p><p>Few examples of Attribute directives are ngStyle, ngClass and ngModel as they just change or deals with the behavior/appearance of the DOM element.</p><h4>You add Attribute directives to the elements as if they are attributes to the elements, hence the name Attribute directive.</h4><pre>&lt;div <strong>[ngClass]=&quot;isSpecial ? &#39;special&#39; : &#39;&#39;&quot;</strong>&gt;<br>          This div is special<br>&lt;/div&gt;</pre><blockquote><strong>Note:</strong><br>I don’t discuss about <strong>ngStyle</strong>, <strong>ngClass</strong> directives as they are <strong>going to be removed</strong> in the future versions of Angular. And to see some examples on <strong>ngModel</strong>, you can see a clear example of <strong>ngModel</strong> and how it helps you to achieve two-way binding without too much of code.</blockquote><h3><strong>Structural directive</strong></h3><p><strong>Structural directive deals with the manipulation of DOM hierarchy, it creates and destroys DOM elements. So, it affects the DOM structure, hence the name Structural directive.</strong></p><p>Structural directives genrally starts with <strong>*</strong> in front of it.</p><pre>&lt;ul&gt;   <br>       &lt;li <strong>*</strong><a href="https://angular.io/api/common/NgForOf"><strong>ngFor</strong></a><strong>=&quot;let hero of heroes&quot;</strong>&gt;{{hero.name}}&lt;/li&gt;<br>&lt;/ul&gt;</pre><p>We are going to some of the built-in structural directives that every beginner should know. I call them <strong>“Conditional structural directives”</strong>. They are</p><ol><li>ngIf</li><li>ngSwitch</li><li>ngFor</li></ol><h3><strong>ngIf</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SmkrReGTkyetWtn_EuCCfg.png" /><figcaption>ngIf</figcaption></figure><p>As you can see, <strong>*ngIf</strong> directive lets us render <strong>name</strong> property based on <strong>show </strong>property. Which means it either creates a DOM element or removes a DOM element, hence altering DOM structure.</p><h4>Why removing it, rather than hiding it?</h4><p>For simple elements it’s okay to hide, but imagine a component which is <strong>resource heavy</strong>( computation heavy ), when we hide it, it still works in the background. Hence removing from the DOM is always better.</p><p><strong>Okay, what about else block?</strong></p><p>You can achieve it by using <strong>ng-template</strong> and <strong>Template reference variable.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uSOe2KTS38f873pByTmL3Q.png" /><figcaption>ngIf with else block</figcaption></figure><p>Wait, what is <strong>ng-template</strong> ?</p><blockquote><strong>ng-template</strong> is <strong>used</strong> to render HTML in a <strong>template</strong> but is never rendered directly. If you add a <strong>ng</strong>-<strong>template</strong> tag to your <strong>template</strong>, it and everything inside it will be replaced by a comment. It might seem a bit useless, but it’s rarely <strong>used</strong> alone. It can be for example <strong>used</strong> to define the else case of an <strong>*ngIf</strong>.<br> — — — — codeburst.io</blockquote><blockquote><strong>Note:</strong><br><strong>ng-template</strong> is a directive.</blockquote><p>As you see from elements tab from console, <strong>ng-template</strong> tag helps us to conditionally render the element, if it fails the condition, it’s get pasted as a comment.</p><pre>@Component({</pre><pre>selector: &#39;app-root&#39;,</pre><pre>template: `</pre><pre>       &lt;h1&gt;Hello&lt;/h1&gt;</pre><pre>       &lt;h1 <strong>*ngIf=&quot;hasSuit; else elseBlock&quot;</strong>&gt;</pre><pre>          Iron man</pre><pre>       &lt;/h1&gt;</pre><pre>       &lt;<strong>ng-template #elseBlock</strong>&gt;</pre><pre>             &lt;h1&gt; Tony Stark&lt;/h1&gt;</pre><pre>       &lt;/<strong>ng-templat</strong>e&gt;</pre><pre>`,</pre><pre>styleUrls: [&#39;./app.component.css&#39;]</pre><pre>})</pre><pre>export class AppComponent {</pre><pre>      <strong>hasSuit = false;</strong></pre><pre>}</pre><p><strong>Okay, what about else if?</strong></p><p>The equivalent of else if can be achieved by separating out the <strong>*ngIf</strong> into separate element and using the <strong>Template reference variabl</strong>e in <strong>ng-template</strong> tag.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BLSpAF1o88D8oje2-TJOjQ.png" /><figcaption>ng with then and else</figcaption></figure><pre>@Component({</pre><pre>selector: &#39;app-root&#39;,</pre><pre>template: `</pre><pre>    &lt;div <strong>*ngIf=&quot;hasSuit; then thenBlock; else elseBlock&quot;</strong>&gt;&lt;/div&gt;<br>    &lt;<strong>ng-template #thenBlock</strong>&gt;<br>         &lt;h1&gt; Tony Stark&lt;/h1&gt;<br>    &lt;/<strong>ng-template</strong>&gt;<br>    &lt;<strong>ng-template #elseBlock</strong>&gt;<br>         &lt;h1&gt; Tony Stark&lt;/h1&gt;<br>    &lt;/<strong>ng-template</strong>&gt;</pre><pre>`,</pre><pre>styleUrls: [&#39;./app.component.css&#39;]</pre><pre>})</pre><pre>export class AppComponent {</pre><pre>   <strong> hasSuit = true;</strong></pre><pre>}</pre><h3>ngSwitch</h3><p><strong>ngSwitch</strong> is as same as the switch statements that we used to see in programming languages .<strong>ngSwitch</strong> helps us to conditionally render if there are many conditions to check through. The syntax is as follows</p><pre>@Component({</pre><pre>selector: &#39;app-root&#39;,</pre><pre>template: `</pre><pre>    &lt;div <strong>[ngSwitch]=&quot;power&quot;</strong>&gt;</pre><pre>        &lt;h1 <strong>*ngSwitchCase=&quot;&#39;thunder&#39;&quot;</strong>&gt;Thor&lt;/h1&gt;</pre><pre>        &lt;h1 <strong>*ngSwitchCase=&quot;&#39;rich&#39;</strong>&quot;&gt;Tony&lt;/h1&gt;</pre><pre>        &lt;h1 <strong>*ngSwitchCase=&quot;&#39;smashing&#39;&quot;</strong>&gt;Hulk&lt;/h1&gt;</pre><pre>        &lt;h1 <strong>*ngSwitchCase=&quot;&#39;big brain&#39;</strong>&quot;&gt;Tony&lt;/h1&gt;</pre><pre>    &lt;/div&gt;</pre><pre>`,</pre><pre>styleUrls: [&#39;./app.component.css&#39;]</pre><pre>})</pre><pre>export class AppComponent {</pre><pre>     <strong>power=&quot;rich&quot;;</strong></pre><pre>}</pre><p>Note the little different different syntax for <strong>ngSwitch </strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tCSZqgkD1u3qlOf38ctZcA.png" /><figcaption>ngSwitch</figcaption></figure><h3>ngFor</h3><p><strong>*ngFor</strong> is same as the for loop that we use in programming languages. It helps us to loop through a series of values. The syntax for <strong>*ngFor</strong> is as follows</p><pre>@Component({</pre><pre>selector: &#39;app-root&#39;,</pre><pre>template: `</pre><pre>      &lt;div <strong>*ngFor=&quot;let char of tonyChar&quot;</strong>&gt;</pre><pre>             &lt;h1&gt;<strong>{{char}}</strong>&lt;/h1&gt;</pre><pre>      &lt;/div&gt;</pre><pre>`,</pre><pre>styleUrls: [&#39;./app.component.css&#39;]</pre><pre>})</pre><pre>export class AppComponent {</pre><pre>   <strong> tonyChar = [&quot;Genius&quot;, &quot;billionaire&quot;, &quot;playboy&quot;, &quot;philanthropist&quot;]</strong></pre><pre>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yktRNYETumnwy082zFIXjQ.png" /><figcaption>ngFor</figcaption></figure><p>Note that we use <strong>Template input variable</strong> here ( <strong>char</strong> ).</p><p>Okay, what if I want to get the index of the element also? what can I do for that? No worries, Angular got you covered. You can access index as follows,</p><pre><strong>*ngFor=&quot;let char of tonyChar;index as i&quot;</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lEWrTCPXI3I75oWzxzPwSw.png" /><figcaption>ng with index</figcaption></figure><p>Like these, <strong>*ngFor</strong> gives you some more options like boolean flag to check whether the element is last or first, the element’s index is odd or even.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Rjh3T01XdfpOUSgEwt4rMA.png" /><figcaption>ng with first element check</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wYOzF_R3TMCoH_Hup7U7uQ.png" /><figcaption>ng with last element check</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dWKkC7Nyb1ox0a59McDZ5w.png" /><figcaption>ng with even index check</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6NdyJ8CHZLtgFqUNcp9sQw.png" /><figcaption>ng with odd index check</figcaption></figure><p>With this, we have reached the end of this article.And as I promised I will be discussing about how to make a custom Directives in the upcoming articles, so don’t worry, I got you covered. See you in the next article.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=54e5f94de350" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bindings in action| Learn Angular]]></title>
            <link>https://medium.com/@yknevenky/bindings-in-action-learn-angular-1a6332f4e444?source=rss-b30af7fe1d58------2</link>
            <guid isPermaLink="false">https://medium.com/p/1a6332f4e444</guid>
            <category><![CDATA[angular-10]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[learn-angular]]></category>
            <category><![CDATA[angular2]]></category>
            <category><![CDATA[angular-binding]]></category>
            <dc:creator><![CDATA[Venky]]></dc:creator>
            <pubDate>Sun, 30 Aug 2020 15:50:19 GMT</pubDate>
            <atom:updated>2020-08-30T15:54:26.883Z</atom:updated>
            <content:encoded><![CDATA[<p>Let’s see Bindings in action.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*iPL-okMKZOQaAd8lg4ONYA.gif" /><figcaption>Don’t do handshakes, the year is 2020.</figcaption></figure><blockquote><strong><em>Note:</em></strong><em><br>This is one of the several articles related to </em><strong><em>“LEARN ANGULAR” series</em></strong><em>, that I create. So if you don’t understand something in this article, kindly have a look at the previous articles.</em></blockquote><p>I make use of the <strong>demo-app</strong> project which we have created in the start of the series.</p><p>To make it more simpler to explain you in a single image, I have removed the <strong>templateUrl</strong> array from the <strong>@Component</strong> decorator in the <strong>app.component.ts</strong> file and replaced with it <strong>template</strong> property. The template property unlike <strong>templateUrl</strong> holds the<strong> HTML</strong> <strong>code</strong> instead of a<strong> path</strong> to the template file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PcgaaIw27WSRp-5CbKod6g.png" /><figcaption>changed code</figcaption></figure><p>Since, we are coding in Typescript and we have to span more lines of HTML template property, I used the back tics <strong>` `</strong> .</p><pre>@Component({</pre><pre>selector: &#39;app-root&#39;,</pre><pre><strong>template: `</strong></pre><pre><strong>&lt;div class=&quot;center&quot;&gt;</strong></pre><pre>     &lt;h1&gt; Hi, how you doing? &lt;/h1&gt;</pre><pre><strong>&lt;/div&gt;</strong></pre><pre><strong>`,</strong></pre><pre>styleUrls: [&#39;./app.component.css&#39;]</pre><pre>})</pre><p>Also, I included a class <strong>center</strong> to the <strong>&lt;div&gt;</strong> and declared the styles in the app.component.css file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wgzuBlI85VPvfYBSlEnlZA.png" /></figure><h3>Let’s look at <strong>Interpolation </strong>and <strong>Property binding </strong>in action</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s12Fy2H9p3gr8iz6dNbCoQ.png" /></figure><pre>@Component({</pre><pre>selector: &#39;app-root&#39;,</pre><pre>template: `</pre><pre>    &lt;div class=&quot;center&quot;&gt;</pre><pre>      &lt;h1&gt; Hi, how you doing?&lt;/h1&gt;</pre><pre>      &lt;p&gt;I am <strong>{{name}}</strong>&lt;/p&gt;</pre><pre>      &lt;p&gt;<strong>{{<em>stringReturningFunction()</em>}}</strong>&lt;/p&gt;</pre><pre>    &lt;/div&gt;<br>`,</pre><pre>styleUrls: [&#39;./app.component.css&#39;]</pre><pre>})</pre><pre>export class AppComponent {</pre><pre>    name = &#39;Venky&#39;;</pre><pre>    disabledProperty = false;</pre><pre>    stringReturningFunction(){</pre><pre>    return &quot;String has been returned from the function using                interpolation&quot;</pre><pre>}</pre><pre>}</pre><p>We can seen that interpolation allows you to incorporate calculated strings, however they have some limitations as discussed in the previous article.</p><p>Using a method that returns a string is fine ( like stringReturningFunction() ), but Interpolation should not involve any side effects.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vRX58rYA3QrYXy7XPSMH4A.png" /></figure><blockquote><strong>Interpolation converts whatever given to it, to a string.</strong></blockquote><p>As you can see that when we use interpolation, the input box is <strong>not enabled</strong>, because <strong>interpolation converts whatever given to it, to a string</strong>. But we need a boolean value to the disabled attribute, hence in this case property binding is preferred.</p><h3>Class binding</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nzRnxOXkZMrEDFlNW0aPUg.png" /><figcaption>class binding</figcaption></figure><pre>@Component({</pre><pre>selector: &#39;app-root&#39;,</pre><pre>template: `</pre><pre>    &lt;div <strong>[class.centeredDiv]=&quot;hasError&quot;</strong>&gt;</pre><pre>       &lt;h1 <strong>[class]=&quot;multiclass&quot;</strong>&gt; Hi, how you doing?&lt;/h1&gt;</pre><pre>       &lt;p&gt;I am venky&lt;/p&gt;</pre><pre>    &lt;/div&gt;</pre><pre>`,</pre><pre>styleUrls: [&#39;./app.component.css&#39;]</pre><pre>})</pre><pre>export class AppComponent {</pre><pre>    name = &#39;Venky&#39;;</pre><pre><strong>    centeredDiv = &quot;center&quot;;</strong></pre><pre><strong>    hasError = true;</strong></pre><pre><strong>    multiclass = &quot;red bgcolor&quot;;</strong></pre><pre>}</pre><p>The <strong>ceneteredDiv</strong> is single class binding property which conditionally get assigned to the class based on <strong>hasError</strong> property.</p><p>The <strong>multiclass</strong> property assigns multiple classes to the class.</p><h3>Style binding</h3><p>Styles can be bounded in three ways, they are</p><ol><li><strong>[style.width]=”width” <em>( </em></strong><em>Single style binding )</em>,</li><li><strong>[style.width.px]=”width” <em>( </em></strong><em>Single style binding with units )</em>,</li><li><strong>[</strong><a href="https://angular.io/api/animations/style"><strong>style</strong></a><strong>]=”styleExpr” <em>( </em></strong><em>Multi-style binding ).</em></li></ol><blockquote><strong>Note:</strong><br>I used width property here, but you can bind whatever CSS property you want to.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J0ezkw9v5zeWuP6ySws3QQ.png" /><figcaption>style binding</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ohINnO9WuH27uQ9jBn27wA.png" /><figcaption>style binding</figcaption></figure><p>I have used all the three ways of style binding and as you have seen, it worked.</p><pre>@Component({</pre><pre>selector: &#39;app-root&#39;,</pre><pre>template: `</pre><pre>    &lt;div [class]=&quot;centeredDiv&quot;&gt;</pre><pre>        &lt;h1 &gt; Hi, how you doing?&lt;/h1&gt;</pre><pre>        &lt;p&gt;I am {{name}}&lt;/p&gt;</pre><pre>        &lt;p [style.fontSize]=&quot;widthWithPx&quot;&gt;I am set by single style   binding&lt;/p&gt;</pre><pre>        &lt;p [style.fontSize.px]=&quot;width&quot;&gt;I am set by single style binding with units&lt;/p&gt;</pre><pre>        &lt;p [style]=&quot; multiStyle&quot;&gt;I am set by multi style binding&lt;/p&gt;</pre><pre>    &lt;/div&gt;</pre><pre>    `,</pre><pre>    styleUrls: [&#39;./app.component.css&#39;]</pre><pre>})</pre><pre>export class AppComponent {</pre><pre>    name = &#39;Venky&#39;;</pre><pre>    width = 70;</pre><pre>    widthWithPx =&quot;40px&quot;;</pre><pre>    centeredDiv = &quot;center&quot;;</pre><pre>    multiStyle = {</pre><pre>        backgroundColor: &quot;red&quot;,</pre><pre>        fontSize: &quot;20px&quot;</pre><pre>    }</pre><pre>}</pre><h3>Attribute binding</h3><p>Attribute binding is to bind attributes to the CLASS. As discussed in the previous articles, there are<strong> some attributes for which there is no equivalent DOM property</strong>, for those attributes if you try to do Property binding you will get an <strong>error</strong>. To rescue us from those cases, Angular provides us the Attribute binding.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*90ENuLj8J_AswJhYN4NwyA.png" /><figcaption>attribute binding</figcaption></figure><p>As you see, <strong>Interpolation</strong> fails here, Angular throws error if you use Property binding syntax here.</p><pre>&lt;div [class]=&quot;centeredDiv&quot;&gt;</pre><pre>    &lt;h1 &gt; Hi, how you doing?&lt;/h1&gt;</pre><pre>    &lt;p&gt;I am {{name}}&lt;/p&gt;</pre><pre>    &lt;table&gt;</pre><pre>       &lt;tr &gt;</pre><pre>          <strong>&lt;td [colspan]={{3}}&lt;/td&gt;</strong></pre><pre>       &lt;/tr&gt;</pre><pre>       &lt;tr&gt;</pre><pre>          &lt;td&gt;1&lt;/td&gt;</pre><pre>          &lt;td&gt;2&lt;/td&gt;</pre><pre>          &lt;td&gt;3&lt;/td&gt;</pre><pre>       &lt;/tr&gt;</pre><pre>&lt;/table&gt;</pre><pre>&lt;/div&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qc_TEga5L5NHfqWsGQgHXQ.png" /><figcaption>attribute binding</figcaption></figure><p><strong>attr</strong>in <strong>[attr.colspan]=”3&quot;</strong> makes it an attribute binding.</p><pre>&lt;div [class]=&quot;centeredDiv&quot;&gt;</pre><pre>   &lt;h1 &gt; Hi, how you doing?&lt;/h1&gt;</pre><pre>   &lt;p&gt;I am {{name}}&lt;/p&gt;</pre><pre>   &lt;table&gt;</pre><pre>     &lt;tr &gt;</pre><pre><strong>        &lt;td [attr.colspan]=&quot;3&quot;&gt;3&lt;/td&gt;</strong></pre><pre>     &lt;/tr&gt;</pre><pre>     &lt;tr&gt;</pre><pre>        &lt;td&gt;1&lt;/td&gt;</pre><pre>        &lt;td&gt;2&lt;/td&gt;</pre><pre>        &lt;td&gt;3&lt;/td&gt;</pre><pre>     &lt;/tr&gt;</pre><pre>   &lt;/table&gt;</pre><pre>&lt;/div&gt;</pre><h3><strong>Event binding</strong></h3><p>Event binding can be achieved by assigning a method of <strong>CLASS</strong>, to an <strong>event</strong>.</p><pre>@Component({</pre><pre>selector: &#39;app-root&#39;,</pre><pre>template: `</pre><pre>  &lt;div [class]=&quot;centeredDiv&quot;&gt;</pre><pre>    &lt;h1 &gt; Hi, how you doing?&lt;/h1&gt;</pre><pre>    &lt;p&gt;I am {{name}}&lt;/p&gt;</pre><pre>     &lt;button <strong>(click)=&quot;changeName()&quot;</strong>&gt;Change the name&lt;/button&gt;</pre><pre>  &lt;/div&gt;</pre><pre>  `,</pre><pre>  styleUrls: [&#39;./app.component.css&#39;]</pre><pre>})</pre><pre>export class AppComponent {</pre><pre>  name = &#39;Venky&#39;;</pre><pre>  centeredDiv = &quot;center&quot;;</pre><pre>  changeName(){</pre><pre>    this.name = &quot;Venkatesan&quot;;</pre><pre>  }</pre><pre>}</pre><p><strong>(click)=”changeName()”</strong> makes the event binding possible.</p><p>Now, click the <strong>Change the name</strong> button and see the magic ( Haha, it’s not magic. It’s <strong><em>Event binding</em></strong> in action).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fj_dzc6q72TFd6A-6pzzug.png" /></figure><h3>Two way binding</h3><p>Two way binding lets us to keep the <strong>Template in sync with the CLASS</strong>.</p><p>As an example, I have used an<strong> </strong><strong>input</strong> element. Once we start typing the value in the <strong>input</strong> element, the view also gets <strong>updated</strong> with the <strong>current value of the input</strong> element. In simple words, the <strong>view is in sync with the CLASS</strong>.</p><p>Here, I have used <strong>ngModel</strong> to let the two way binding do the magic, but for that you need to import the <strong>FormsModule</strong> from <strong>@angular/forms</strong> library and add it to the imports array of <strong>NgModule</strong>.</p><pre><strong>&lt;input [(ngModel)]=&quot;name&quot; type=&quot;text&quot;&gt;</strong></pre><p><strong>[(ngModel)]=”name” </strong>helps to keep the CLASS property <strong>name</strong> in sync with template.</p><blockquote><strong>Note:</strong><br><strong>”name”</strong> is same as CLASS property name and it should be the case. They should be same to let the Angular know the connection.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ifM0-lWg_ZR9o2pWjvfOjg.png" /><figcaption>TWO WAY BINDING</figcaption></figure><pre>@Component({</pre><pre>selector: &#39;app-root&#39;,</pre><pre>template: `</pre><pre>  &lt;div [class]=&quot;centeredDiv&quot;&gt;</pre><pre>      &lt;h1 &gt; Hi, how you doing?&lt;/h1&gt;</pre><pre>      &lt;p&gt;I am {{name}}&lt;/p&gt;</pre><pre>      <strong>&lt;input [(ngModel)]=&quot;name&quot; type=&quot;text&quot;&gt;</strong></pre><pre>  &lt;/div&gt;</pre><pre>  `,</pre><pre>  styleUrls: [&#39;./app.component.css&#39;]</pre><pre>})</pre><pre>export class AppComponent {</pre><pre>  name = &#39;Venky&#39;;</pre><pre>  centeredDiv = &quot;center&quot;;</pre><pre>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*ior510fEuugbzLyZiCp09g.gif" /><figcaption>Two way binding</figcaption></figure><p>With this we have reached the end of the article, now we have learned how <strong>Binding</strong> works in <strong>Angular</strong>.</p><p>See you in the next article!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1a6332f4e444" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Template syntax | Learn Angular]]></title>
            <link>https://medium.com/@yknevenky/template-syntax-learn-angular-b29e9173bac4?source=rss-b30af7fe1d58------2</link>
            <guid isPermaLink="false">https://medium.com/p/b29e9173bac4</guid>
            <category><![CDATA[binding]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[learn-angular]]></category>
            <category><![CDATA[interpolation]]></category>
            <category><![CDATA[angular-template]]></category>
            <dc:creator><![CDATA[Venky]]></dc:creator>
            <pubDate>Sat, 29 Aug 2020 21:31:37 GMT</pubDate>
            <atom:updated>2020-08-30T06:02:03.173Z</atom:updated>
            <content:encoded><![CDATA[<p>Learn Template syntax of Angular.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*51pjzOmvoY8CjeQvK6PjWw.png" /><figcaption>Template syntax</figcaption></figure><blockquote>This is one of the several articles related to <strong>“LEARN ANGULAR” series</strong>, that I create. So if you don’t understand something in this article, kindly have a look at the previous article.</blockquote><p>This article is sort of <strong>‘very theory heavy’</strong>. So, have some patience and a chill mind to explore. The upcoming concepts will really make a lot of things to click in your mind.</p><h3>What is Template syntax?</h3><blockquote><strong>HTML SYNTAX ON STEROIDS!!</strong></blockquote><blockquote>To simply put, Template syntax is a syntax which gives much more functionality and options than our default HTML syntax.</blockquote><p>For example, Angular helps you to get and set DOM (Document Object Model) values dynamically with features such as built-in template functions, variables, event listening, and data binding.</p><h3>What is a template?</h3><blockquote><strong>HTML on Steroids !!</strong></blockquote><blockquote>In Angular, a <em>template</em> is a chunk of HTML. Within a template, you can use special syntax to leverage many of Angular’s features.</blockquote><p>So, it’s nothing but an HTML. Every valid HTML syntax is a valid template syntax in angular. However to eliminate the risk of script injection attacks, Angular does not support the &lt;script&gt; element in templates.</p><p>So, the job of a Template is to give a structure to the view as HTML does. But, Template has much more functionalities provided by Angular.</p><h3>How does Template syntax does this?</h3><blockquote><em>It does so by several core concepts given below, we will discuss each of them in detail,</em></blockquote><blockquote><strong><em>1. Interpolation,</em></strong></blockquote><blockquote><strong><em>2. Template expression,</em></strong></blockquote><blockquote><strong><em>3. Template statements,</em></strong></blockquote><blockquote><strong><em>4. Binding.</em></strong></blockquote><h3>1. What is Interpolation?</h3><blockquote><em>Interpolation helps you to </em><strong><em>incorporate calculated strings</em></strong><em> into the text </em><strong><em>between HTML element tags</em></strong><em> and </em><strong><em>within attribute assignments.</em></strong></blockquote><p>So, interpolation is basically a way to embed strings into the <strong><em>HTML</em></strong> element and assign string values to the template attributes.</p><p>&lt;h1&gt;{{title}}&lt;/h1&gt; here interpolation allows us to insert title’s value as heading.</p><p>&lt;img src=”{{imgUrl}}” &gt; here interpolation allows you to assign imgUrl’s value into the src attribute.</p><h3><strong>2. What is Template expression?</strong></h3><blockquote>The expressions that are used in templates are called Template expressions. Hence, the name Template expressions.</blockquote><blockquote><strong>Note:</strong><br>And Template expressions <strong>can not </strong>have side effects.</blockquote><p>The thing that we put side Interpolation braces {{}}are expressions. Template expressions are much like Javascript expressions but with some restrictions.</p><blockquote>A template expression produces a value and appears within the double curly braces, {{ }}.</blockquote><p>Angular executes the expression and assigns it to a property of a binding target.</p><blockquote>Binding target means, things such as an element, component, or directive</blockquote><p>The interpolation braces in {{ title }} surround the template expression title. In the property binding, a template expression appears in quotes to the right of the = symbol as in [property]=&quot;expression&quot;.</p><p>As I said, Template expressions are much like Javascript expressions but with some limitations, you can’t use JavaScript expressions that have or promote side effects, including:</p><ul><li>Assignments (=, +=, -=, ...)</li><li>Operators such as new, typeof, instanceof, etc.</li><li>Chaining expressions with ; or ,</li><li>The increment and decrement operators ++ and --</li><li>Some of the ES2015+ operators.</li></ul><p>The &quot;expression&quot;will refer to different things, depending on the context you write, it can be</p><ul><li>Template input variable,</li><li>Template reference variable.</li></ul><p><strong>Template input variable</strong></p><blockquote><em>The expression is a Template input variable, if the expression contains a variable of a template.</em></blockquote><pre>&lt;ul&gt;<br>      &lt;li *<a href="https://angular.io/api/common/NgForOf">ngFor</a>=&quot;let customer of customers&quot;&gt;{{customer.name}}&lt;/li&gt;<br>&lt;/ul&gt;</pre><p>The customer variable from *ngFor is <strong>fed as an input</strong> to the interpolation braces, hence it is called <strong>Template input variable.</strong></p><p><strong>Template reference variable.</strong></p><blockquote>Template reference variable is a variable that contains a reference to a Template, a DOM element.</blockquote><p><em>Often times, we might want to </em><strong><em>have a reference to a DOM element</em></strong><em>, for example </em><strong><em>changing that DOM element’s properties and style from some other template</em></strong><em>. Template reference variable can help us in those situations. Template reference variables has a prefix of #.</em></p><pre>&lt;input #phone placeholder=&quot;phone number&quot; /&gt;</pre><pre>&lt;button (click)=&quot;callPhone(phone.value)&quot;&gt;Call&lt;/button&gt;</pre><p>In the above code, #phone is a Template reference variable, as we can see, we can access the &lt;input&gt; element with #phone in the &lt;button&gt; element to pass it to the click event handler.</p><h3>3. What is Template statement?</h3><blockquote>Template statements are statements that gets executed whenever there is an event raised by the binding target element.</blockquote><blockquote><strong>Note:</strong><br>Template statement <strong><em>has a side effect</em></strong>. That’s the whole point of an event. It’s how we can update application state from user action.</blockquote><p>Template statement appears in quotes to the right of the = symbol as in (event)=&quot;statement&quot;.</p><pre>&lt;button (click)=&quot;deleteUser()&quot;&gt;Delete hero&lt;/button&gt;</pre><p>Template statements have the same limitations as Template expressions.</p><h3>4. What is binding ?</h3><blockquote>Binding is, what it sounds like, binding the things. Here, binding is like a connection that you define between a Template element and an action, to perform some changes and the Angular will take care of the rest.</blockquote><p>Binding in Angular is classified into three major categories base on which is connected to which, they are</p><ul><li>From <strong>CLASS</strong> to <strong>TEMPLATE ( Property Binding ),</strong></li><li>From <strong>TEMPLATE</strong> to <strong>CLASS</strong> <strong>( Event Binding )</strong>,</li><li>Two way binding <strong>CLASS</strong> to <strong>TEMPLATE</strong> to <strong>CLASS .</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LvMniJKqOgd3aw1IybOwzQ.png" /><figcaption>Binding in Angular</figcaption></figure><h3>Generally there are two kinds of bindings, they are</h3><ol><li>One-way binding,</li><li>Two-way binding.</li></ol><p><strong>One-way binding</strong></p><p>One-way binding means that the flow is in <strong>only one direction</strong> as described in the picture above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j4cb4J0Oyk8OyeEyGBNdOw.png" /><figcaption>One way binding in Angular</figcaption></figure><p><strong>Property binding</strong></p><p>Before getting started with property binding, it is mandatory that we should know the difference between <strong>attributes</strong> and <strong>properties</strong>.</p><blockquote><strong><em>Attributes</em></strong> are defined by <strong><em>HTML</em></strong>,<br>while <strong><em>Properties</em></strong> are defined by <strong><em>DOM</em></strong>.</blockquote><blockquote>Attributes initializes the DOM properties.</blockquote><blockquote>So the value of an <strong>Attribute</strong> is <strong><em>different</em></strong> from the value of a <strong>Property</strong>.</blockquote><p>— — — —</p><h3>NOTE:</h3><blockquote>Interpolation and property binding can set only <em>properties</em>, not attributes.</blockquote><p>— — — —</p><p>I advice you to go through this <a href="https://angular.io/guide/binding-syntax#html-attribute-vs-dom-property">link</a> for more details about <strong>HTML attributes </strong>Vs <strong>DOM properties.</strong></p><h4>Now about property binding,</h4><blockquote>Property binding means<strong> binding CLASS properties</strong> with target element <strong>properties</strong>.</blockquote><blockquote>[property] = “expression”</blockquote><pre>&lt;img [src]=&quot;itemImageUrl&quot;&gt;</pre><p>The above code, assigns the itemImageUrl (property of a class ) to srcproperty of &lt;img&gt;.</p><h4><strong>Attribute binding</strong></h4><p>As there is no 1:1 mappings between Attributes and Properties, if you want to create an attribute and attach a value to it, you can do that with attribute binding.</p><pre>&lt;button [attr.aria-label]=&quot;actionName&quot;&gt;{{actionName}} with Aria&lt;/button&gt;</pre><p>Since, there is no corresponding DOM property for aria-label attribute, we cannot do property binding here. We can do attribute binding as mentioned above, though. Note attr in [attr.aria-label], it is needed to, do attribute binding.</p><h4><strong>Class binding</strong></h4><p>As you might have guessed, it’s nothing but binding the classes defined in <strong>CLASS</strong> properties to the<strong> HTML</strong> class properties.</p><p>There are 2 ways of doing class binding, they are</p><blockquote>[class]=”classExpression”</blockquote><blockquote>[class.foo]=”hasFoo”</blockquote><p><em>”classExpression” in the first one can be a string containing series of classes, </em>and “hasFoo” can be a boolean property from a CLASS, to conditionally attach .foo to the class property.</p><blockquote>Since ngClass directive doesn’t provide any significant value and going to be removed by Angular in future, I don’t discuss it.</blockquote><h4>Style binding</h4><p>Style binding allows you to bind your <strong>styles defined</strong> in <strong>CLASS </strong>property to the <strong>style property of an element</strong>.</p><p>There are 3 ways of binding styles to the element, they are</p><ol><li>[style.width]=”width” ( Single style binding ),</li><li>[style.width.px]=”width” ( Single style binding with units ),</li><li>[<a href="https://angular.io/api/animations/style">style</a>]=”styleExpr” ( Multi-style binding, where<br> ”styleExpr” = “width: 100px; height: 100px”).</li></ol><h4>Event binding</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4YcRDdt63bBSmu-THyH4lQ.png" /><figcaption>Event binding</figcaption></figure><p>Event binding allows you to bind an event to a property or method of a <strong>CLASS</strong>, so that whenever an event is raised by the target element it can perform some operations.</p><pre>&lt;button (click)=&quot;onSave()&quot;&gt;Save&lt;/button&gt;</pre><p>The above code calls onSave() , when a (click) event is registered on the button. You can also pass the event object to the method as a parameter as follows.</p><pre>&lt;button (click)=&quot;onSave($event)&quot;&gt;on-click Save&lt;/button&gt;</pre><p>Alternatively, use the on- prefix, known as the canonical form:</p><pre>&lt;button on-click=&quot;onSave($event)&quot;&gt;on-click Save&lt;/button&gt;</pre><p>Sometimes, all you want to do for an event, is to change property’s values.In that case, you can do as follows.</p><pre>&lt;input [value]=&quot;currentItem.name&quot;<br>       (input)=&quot;currentItem.name=$event.target.value&quot; &gt;</pre><p>So we can choose any of these ways to write our Event binding.</p><h3>Two-way binding</h3><p>Two way binding refers to the bidirectional flow between the <strong>CLASS</strong> and <strong>TEMPLATE</strong>.</p><h4>Two-way binding does two things:</h4><blockquote>1. Sets a specific element property.</blockquote><blockquote>2. Listens for an element change event.</blockquote><p>Two way binding has a syntax <strong>[( )]</strong> called banana box. Yes, Angular calls that way. Because, it looks like <strong>banana</strong> <strong>()</strong> inside a <strong>box</strong> <strong>[]</strong>.</p><p>The <strong>[()]</strong> syntax combines the brackets of property binding, [], with the parentheses of event binding, <strong>()</strong>.</p><p>To implement two way binding, you need to put the event you want to listen inside <strong>()</strong>, and put the property( CLASS property )you want to keep in sync with element inside <strong>[]</strong>.</p><pre>&lt;div [(sizeChangeEvent)]=&quot;fontSizePx&quot;&gt;&lt;/div&gt;</pre><p>Here <strong>sizeChangeEvent</strong> can be a method that changes the font size and <strong>fontSizePx</strong> can be a property defined in CLASS to keep track of current font size.</p><p>With this special type of <strong>[()]</strong> syntax, Angular will automatically wire up the two way binding, to keep the TEMPLATE and CLASS properties in sync.</p><p>That’s enough of the theory about Binding.With this, we have reached the end of this article.</p><p>In the next article, we will implement these concepts and look in practical.</p><p>Pat on your shoulders for sticking through the end, now you know enough things, about <strong>Template Syntax of Angular.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b29e9173bac4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learn Angular!]]></title>
            <link>https://medium.com/@yknevenky/learn-angular-d4a1791c7a9a?source=rss-b30af7fe1d58------2</link>
            <guid isPermaLink="false">https://medium.com/p/d4a1791c7a9a</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[angular-tutorial]]></category>
            <category><![CDATA[ng]]></category>
            <category><![CDATA[angular-cli]]></category>
            <category><![CDATA[learn-angular]]></category>
            <dc:creator><![CDATA[Venky]]></dc:creator>
            <pubDate>Fri, 28 Aug 2020 19:04:04 GMT</pubDate>
            <atom:updated>2020-10-07T09:24:14.835Z</atom:updated>
            <content:encoded><![CDATA[<p>An Angular series for beginners.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eVkLZBgCLeeEV4IOkZdK2g.png" /><figcaption>Angular site</figcaption></figure><blockquote><strong>Note:</strong><br>This series is not created with a goal of learning some particular version of <strong>Angular</strong>. It is created with the <strong>goal of covering the core concepts of Angular</strong>, so even if a new version of Angular gets released, this series will still remain valid. If any future version brings out breaking changes, I will update it as soon as I could.</blockquote><p>Hey, I have been thinking for a long time, to create a series for learning angular concepts for beginners to get started with <strong>Angular</strong> and explore further.</p><p>The motivation behind this series is to communicate angular concepts to you in a way that I think, it is easily digestible for a beginner and keep myself occupied with making more series to learn development related concepts and technologies.</p><p>Let’s get into the Angular series</p><blockquote><strong>Note: <br></strong>This article is one of many articles that I will be posting from now (29/08/2020) on about angular. So, this article covers only, how to get started with angular and an overview of how angular works.</blockquote><h3>Get started with angular</h3><h4>What is Angular?</h4><blockquote>Angular is an <strong>application design framework</strong> and <strong>development platform</strong> for creating efficient and sophisticated single-page apps.</blockquote><p>Things to note here are,<br>Angular is a <strong>framework</strong> to <em>build client-side applications</em> and also it <strong>provides a development environment</strong> to create efficient and sophisticated single page applications.</p><p><strong>Note:</strong><br>Often times, you will hear that, <strong><em>REACT</em></strong> is an alternative to Angular, and it’s true. But one thing you have to note that <a href="https://reactjs.org/"><strong>REACT is a library</strong></a>, and <strong>Angular is a framework</strong>. This is the reason why you can plugin React code into an existing html/js based code but not angular code, because Angular is a framework, <strong>not a library</strong>.</p><h3>It’s important to know a itty bitty history of angular!</h3><p><strong>AngularJS</strong> was created, as a side project, in 2009 by two developers, <a href="https://twitter.com/mhevery"><strong>Misko Hevery</strong></a> and <a href="https://twitter.com/abrons"><strong>Adam Abrons</strong></a><strong> at Google</strong>.</p><p>Note, it is AngularJS, not Angular. Because, the Angular, that initially developed by Google devs is considerably different from the current version of Angular. <br>So,<strong> AngularJS</strong> refers to Angular 1.xx. Even though, Angular was pretty successful for it’s template syntax and many other reasons, it had some considerable issues with it. The major issues with it were it’s huge bundle size and unpredictable performance problems. So, Google devs found a motivation to develop <strong>Angular 2.xxx</strong> or <strong>Angular 2</strong>.</p><p>Angular 2 is the one which is similar to that of today’s Angular.</p><p>Then came the Angular 4, Angular 5, Angular 6 and so on..</p><p>Angular 3 was skipped due to version number changes in the repo of angular.</p><blockquote><em>Angular uses </em><strong><em>semantic versioning</em></strong><em>, releasing a major version in every </em><strong><em>6 months.</em></strong></blockquote><blockquote><em>Say, version number </em><strong><em>A.b.c</em></strong><em>, where</em></blockquote><blockquote><em>A— major version number ( say a huge update ),<br>b — minor version number( say a feature addition ),<br>c — patch number ( say to fix some issues ).</em></blockquote><p>Angular uses Typescript language from the Microsoft. So, knowledge of Typescript will help you to understand Angular better. But, it’s not mandatory, as long as you have solid knowledge of JAVASCRIPT. After all, Typescript is a super set of Javascript.</p><h3>Enough story, let’s get ourselves into the core</h3><p>You cannot learn Angular without actually doing the Angular.Even though this series will explain everything you need, but I think there is no better way to learn Angular, than by doing the things of Angular, understanding it and see it working. So, let’s start coding.</p><p>To work with Angular, there are some prerequisites that you need to have, they are</p><ol><li>Node js,</li><li>npm</li></ol><p>So, make sure you have them installed on your machine.</p><p>I use VS CODE as my IDE, to write angular apps. It is the goto editor of choice for developing web applications.</p><h4>Angular installation</h4><pre>npm install -g @angular/cli</pre><p>Run the above command in your terminal.</p><p>It will ask some couple of questions like, whether you want to add routing and all. Just breeze through the defaults by pressing Enter.</p><p>Now, Angular is installed on your machine.</p><p><strong>Wait a minute, what’s that </strong><strong>/cli?</strong></p><p>What you have actually installed is Angular CLI, which is a command line tool to generate building blocks of Angular to make development quicker and easy. Remember that Angular provides a development environment ? Here, it is.</p><p>To check whether the angular successfully installed or not, run this following command</p><pre>ng --version</pre><p>If it results in</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/483/1*9kv4PxASlO2_LfvjXuYCIQ.jpeg" /><figcaption>Angular installation success</figcaption></figure><p>then, you are good to go. Else, drop in the problem in the comment section, I will try to help.</p><h4>Creating an Angular application</h4><p>To create a new angular app, run this command.</p><pre>ng new demo-app</pre><p>ng stands for angular, new stands for new application. demo-app is the application name.</p><p>It will take some time for ng to create the application, so wait for a while. After the app has been created, the project structure will look something similar to this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/1*QBIXH4cLb9vTfGi5oL418Q.jpeg" /><figcaption>Folder structure</figcaption></figure><p>Knowing about some of these folders and files will help you in understanding the Angular application better, so let’s know some of it.</p><ol><li><strong>e2e </strong>- used for End to End testing,</li><li><strong>package.json</strong> - file used by node’s npm to keep track of the dependencies and dev dependencies of our angular application,</li><li><strong>node_modules</strong> - contains the dependency files related to the application,</li><li><strong>src </strong>- this is the directory where we code our app,</li><li><strong>tsconfig.json</strong> - Code editors and TypeScript’s language server use this file to improve development experience. Compilers do not use this file,</li><li><strong>tsconfig.app.json</strong> - Application-specific TypeScript configuration, including TypeScript and Angular template compiler options,</li><li><strong>tsconfig.spec.json</strong> - TypeScript configuration for the application tests.</li></ol><p>Before going to explore src/ folder structure, let’s understand the high level architecture of angular.</p><h3><strong>A high level architecture of Angular app in general</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nMyKPC3Ig7sOS3MdAO9c3g.png" /><figcaption>Angular architecture</figcaption></figure><p>Angular apps are modular and Angular has its own modularity system called <strong>NgModules</strong>.</p><h3>NgModules</h3><blockquote>NgModules are the building blocks of any Angular app.</blockquote><p><strong><em>NgModules</em></strong> provide a <strong>compilation context</strong> for <em>components</em>. <strong>NgModules</strong> collect related code into functional sets; an Angular app is defined by a set of <strong>NgModules</strong>.</p><p><strong>Compilation context ? What does that mean ?</strong></p><p>Compilation context is nothing but the files and components that the app needs to get compiled by Typescript. It implies that the context contains everything a compiler needs to compile without any errors. And NgModules provides you that context.</p><blockquote>NgModules are containers for a cohesive block of code dedicated to an application domain, a workflow, or a closely related set of capabilities.</blockquote><p>Every Angular app has at least one NgModule class, the <strong><em>root module</em></strong><em>.</em></p><p>While a small application might have only one NgModule ( root module ), most apps have many more <strong><em>feature modules</em></strong>.</p><blockquote>These modules will interact and ultimately render the view in the browser.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6T9lZbkoVDZFmOuHB3Qm8Q.png" /></figure><ul><li>Angular app consists of <strong>Modules</strong>,</li><li><strong>Modules</strong> consists of <strong>Components</strong> and <strong>Services</strong>,</li><li><strong>Components</strong> in turn can consists of <em>several other</em> <strong>components</strong>,</li><li>Each <strong>Component</strong> consists of an <strong>HTML TEMPLATE</strong> and a <strong>CLASS</strong>,</li><li><strong>HTML TEMPLATE</strong> is what it is, an HTML,</li><li><strong>CLASS</strong> is something like ts/js file, it contains the <strong>logic of the Component</strong>,</li><li><strong>Services</strong> contains the <strong>business logic</strong> of the application.</li></ul><blockquote><strong>EVERY ANGULAR APP WILL HAVE ROOT MODULE, AND SO A ROOT COMPONENT AS WELL.</strong></blockquote><p>CONVENTIONALLY, THE ROOT MODULE IS CALLED AS <strong>APP MODULE</strong> AND THE ROOT COMPONENT IS CALLED <strong>APP COMPONENT</strong>.</p><p>The <em>root</em> NgModule for an app is so named because it can include<strong> child NgModules</strong> in a hierarchy of any depth.</p><h4>Now let’s explore src/ folder</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/488/1*ub4abAFB6-HV2HOiBIf71w.jpeg" /><figcaption>src folder structure</figcaption></figure><p>All our code goes into the src folder.</p><p>Here you can see the module app.module.ts,the component app.component.ts,the html app.component.html, css app.component.css.</p><p>There are three files for each and every component that you create with Angular and it will always be the case, they are</p><ol><li>a Typescript file ( containing logic ),</li><li>an Html file ( containing structure of the component ),</li><li>a style file ( can be <strong>scss</strong>, <strong>css</strong> etc for styling of the component ).</li></ol><p>Every component that you create, will go with it’s own folder, under the app folder.</p><p>So for every component, there will be three files as mentioned above. We will explore them in the next article. Don’t worry. Before going further, I want to introduce you the <strong>Decorators</strong>.</p><h4>Decorators</h4><p>Decorators are the <strong>functions</strong> that can be used to a<strong>dd meta-data, methods</strong> and <strong>properties</strong> to the thing that they are attached to.</p><blockquote>Modules, components and services are <strong>classes</strong> that use <strong><em>Decorators</em></strong>.</blockquote><blockquote>These decorators mark their type and provide <strong>metadata</strong> that tells Angular <strong>how to use</strong> them.</blockquote><p>In Angular you can see decorators like @Component @NgModule @Injector @Input . These decorators transforms a <strong>class</strong> that they are attached to, to something by providing meta-data, properties and methods. For example, @Component decorator above a class is, what makes a class, an Angular component.</p><h3>Let’s look at the flow</h3><p>By flow what I mean is that how the stuffs are connected.</p><p>Let’s start at main.ts,</p><ol><li>main.ts</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1BVyTDFSnvIDa7zB5DlbuQ.png" /><figcaption>main.ts file</figcaption></figure><p>Here, as you can see <strong>AppModule</strong> from app.module.ts is passed through the bootstrapModule method, therefore you can say, that the control flows through the app.module.ts file.</p><p>2. app.module.ts</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O42vv1XMiu1Xv1Z-DFX3Zw.png" /><figcaption>app.module.ts</figcaption></figure><p>Here, <strong>AppComponent</strong> from app.component.ts is passed into the declarations array. A point to note here is that, whatever the component you create, must be declared in this declarations array. Also, each module file will have a <strong>Decorator </strong>called @NgModule . Since, <strong>AppComponent</strong> is declared here, we can say that the control then goes to the app.component.ts file.</p><p>3. app.component.ts</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OKwJMEE7nNzSO6IMj0v0Xw.png" /></figure><p>In this file, as you can see that, <strong>html </strong>and <strong><em>style files</em></strong> has been referenced in the templateUrl field and styleUrls array. Also, note the <strong>selector</strong> property having the value ‘app-root’ . Now, take a look at index.html file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*byqKc-lNHs0_bjgD_m4p7w.png" /><figcaption>index.html</figcaption></figure><p>Bamm , got it? Nope? Don’t worry, let me explain it. What is going on is, that the @Component in the app.component.ts will look for &lt;app-root&gt; tag in the html files. Once it finds it, Angular will cut and paste the app.component.html code inside the &lt;app-root&gt; tag. Pretty cool, right?</p><p>Now that you have a good grasp of how the code works, let’s run our app and see what we get.</p><p>To run the app, you need to run</p><pre>ng serve</pre><p>It will ask you, whether you are okay with sharing the data related to angular anonymously. You can give Yes or No(default one). After that, you will get a log message like</p><blockquote>* Angular Live Development Server is listening on localhost:4200, open your browser on <a href="http://localhost:4200/">http://localhost:4200/</a> ** : Compiled successfully.</blockquote><p>Now, open the browser of your choice and enter the url given by the angular, in my case <a href="http://localhost:4200/">http://localhost:4200/</a> . You will get the default Angular app generated by Angular CLI as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YClO9S6av2bGRpPXJBBoLg.png" /><figcaption>Default angular app generated Angular CLI.</figcaption></figure><p>Now, for some fun, let’s delete everything from the app.component.html file and just add the code given below. Save the file and see the page.</p><pre>&lt;h1&gt;{{title}}&lt;/h1&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AGAL4fw8-KHxwllD12XpPA.png" /></figure><p>You just see the app name right? in my case <strong>demo-app</strong>.</p><p>Where does this title comes from? what are those curly braces?</p><p>The title comes from your app.component.ts file. Look at the title property under <strong>AppComponent</strong> class, that is where the title comes from.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aSqE0AC5yrAovYIsguvCVA.png" /><figcaption>app.component.ts</figcaption></figure><p>Now change the title to whatever string you want, save the file and see the page. It will render the string that you gave to the title.</p><p>Some little points to add</p><ul><li>The presence of @NgModule means that it is a module file,</li><li>The presence of @Component means that it is a component file,</li><li>Every components that you create must be declared in the declarations array of @NgModule <strong>Decorator.</strong> But you will see that why you don’t need do that by yourself in the upcoming articles.</li></ul><p>Congratulations, now you know enough little things about Angular to create your own Angular app.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d4a1791c7a9a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why prototypal inheritance in javascript is more of a delegation thing, than an inheritance itself?]]></title>
            <link>https://medium.com/@yknevenky/why-prototypal-inheritance-in-javascript-is-more-of-a-delegation-thing-than-an-inheritance-itself-12d65dc0525d?source=rss-b30af7fe1d58------2</link>
            <guid isPermaLink="false">https://medium.com/p/12d65dc0525d</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[classical-inheritance]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[prototypal-inheritance]]></category>
            <category><![CDATA[composition]]></category>
            <dc:creator><![CDATA[Venky]]></dc:creator>
            <pubDate>Thu, 14 May 2020 17:43:08 GMT</pubDate>
            <atom:updated>2020-05-15T05:35:07.071Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>Understanding the prototypal inheritance in javascript.</strong></h4><blockquote><strong>Javascript is based on prototypal inheritance.</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wAr38VNviSLILlCNJ8pyKg.png" /></figure><blockquote>In this article, I will answer the question after going through a series of some essential concepts, I might jump into some concepts which might make you feel alienated from the objective of this article, so don’t get frustrated, it’s okay to get confused and feel lost. At the end, everything will be clear, assuming I do a good job( Lol, I will, I hope so). And one more disclaimer is that, I will use the word inheritance instead of the word delegation until the end of the article because personally I feel it’s the best way to answer the title of this article.</blockquote><p>As I said, to answer the title, first we need to boil the questions like, what inheritance is?, what are the types of inheritance at the highest level, particularly with respect to design patterns and understanding the difference between them.</p><h4><strong>What is inheritance?</strong></h4><blockquote><strong>Inheritance is way of code reuse, it’s that simple.</strong></blockquote><blockquote><strong>A more clear way to quote is, It’s a code reuse mechanism, which achieves IS-A relationship between the entities.</strong></blockquote><p>What is IS-A relationship?</p><p>Simply put, here is the example,</p><blockquote><em>Dog</em> <strong>IS-A<em> </em></strong><em>Animal, ( Class Dog extends Animal )</em></blockquote><p>You would also come up with a definition like, it’s way of getting the properties of one object from another object. It’s not wrong, but it’s not a definition at the higher level( not a general definition ) and it doesn’t explain the purpose of inheritance.</p><h4>What are the types of inheritance?</h4><ol><li><em>Class based inheritance</em></li><li><em>Prototype based inheritance</em></li></ol><p>Most of you might have surprised to see this classification of inheritance, especially if you have a background of class based inheritance languages like C, C++, Java etc. If you aren’t surprised comment an Hi, let’s have a discussion.</p><p>This classification of inheritance into class based and prototype based is a design oriented way of classification.</p><p><strong>First of all what is prototypal inheritance?</strong></p><p>To understand what exactly is prototypal inheritance, let’s talk about the classical inheritance, the inheritance based on classes.</p><p><strong>Classical inheritance:</strong></p><p>Classical inheritance is what we generally do in OOP languages such as C, C++, JAVA.</p><blockquote>Here, we create classes( blueprints ), and based on that class we create objects, and we use that class to create another class, a sub class( a sub blueprint ) inheriting all the members in that class, that are accessible to the subclass.</blockquote><p>let’s talk about prototypal inheritance, shall we?</p><p><strong>Prototypal inheritance:</strong></p><p>Prototypal inheritance is surprisingly a much simpler inheritance than the class based inheritance. Are you ready to hear the definition of prototypal inheritance? Here you go,</p><blockquote>Prototypal inheritance is a type of inheritance that allows inheriting from another object itself, rather than a class, prototypal inheritance doesn’t have a class concept, a blue print. Rather it creates an object from an existing object (a prototype)itself.</blockquote><p>I said that the prototypal inheritance is much simpler than class based inheritance, because in class based inheritance we need to have a class to create an object, and have to create another class to inherit from the existing class, so that we could create an object having properties of both the classes.</p><p>Whereas in prototypal inheritance, we can create an object from another object inheriting all the properties of the first object( a prototype ).</p><p><strong>What the hell on earth does the prototype mean?</strong></p><p>It’s hard to come up with a strict definition of prototype. But you can take it as an object, from which you can create an object, but you cannot bring the parent class analogy of a class based inheritance to a prototype because the prototype is not a class, yet is an abstraction of an real world entity.</p><p><strong>To understand the differences between the classical and prototype based inheritance, we will see what is the problem with inheritance</strong>. And how a concept called COMPOSITION might rescue from those problems.</p><p>I told you, I would jump from concept to concept, bear with me because it’s more interesting than you can imagine.</p><h3>Experienced developers always say,</h3><blockquote>GO WITH THE <strong>COMPOSITION</strong> OVER <strong>INHERITANCE</strong>, because inheritance is a <strong>tightly coupled relationship</strong>. And in software design, we have to <strong>achieve loose coupling.</strong></blockquote><p>Here is the developer way of explaining what composition and inheritance is,</p><blockquote><strong>Inheritance</strong> is when you <strong>define your logic</strong>, based on <strong>what they are?</strong></blockquote><blockquote>and <strong>Composition</strong> is when you <strong>define your logic</strong>, based on <strong>what they do?</strong></blockquote><p>Also,</p><blockquote>“Inheritance is the most tightly coupled relationship one could achieve, because to inherit from a parent, the child has to know the implementation of the parent, which is a concrete one(a tightly coupled relationship rather than an abstraction) as opposed to the loosely coupled principle”.</blockquote><blockquote><strong>The goal should always be to design an interface, rather than an implementation ( which is a class ).</strong></blockquote><h3>What is COMPOSITION?</h3><blockquote>Composition is also a code reuse mechanism, but it achieves <strong>HAS-A </strong>relationship.</blockquote><p>Here is an example,</p><blockquote>A car <strong>HAS-A</strong> engine.</blockquote><p>Composition is what exactly it sounds like. Composition of entities, A car comprises an engine in it.</p><p>CAR = BODY + ENGINE + WHEELS + etc</p><p>Car is a composition of body, engine and wheels</p><h4>How composition is better than inheritance?</h4><p>If you design with composition in mind, what you will end up with is, a discrete set of functions with it’s own, self contained functionalities.</p><blockquote>Note: Composition is an important concept in functional programming, and javascript is also a functional programming language.</blockquote><p>So you could compose only the necessary functions and achieve the goal. But <strong>if you design your logic based on inheritance, there is a rigid structure. </strong>By the term rigid what I mean is that, it’s hard to change the logic if it’s based on inheritance.</p><p>Let’s say Class A inherits from Class B, and Class B inherits from C.</p><p>Here there are two problems</p><ol><li>You may not need the properties of A for an object of type C, but due to the definition of what class is, if you instantiate C, it will have a copy of properties of B which in turn have the properties of A.</li><li>Another problem is, if you want to have an object that has to have the properties of A and C alone, you are screwed, because unless you redefine the hierarchy of these classes and add some modifications, you cannot create the object that you need.</li></ol><p>But when it comes to composition, there is no need to worry about, because you can use the function of A and function of C and compose it together to achieve the functionality that is required.</p><blockquote><strong>Note:</strong> the problem one of inheritance is called “<strong>The duplication by necessity</strong>”</blockquote><blockquote>the problem two is called <strong>“Inflexible hierarchy problem”.</strong></blockquote><p>In a nutshell, what composition tries to solve is <strong>GORILLA/BANANA PROBLEM. What it means is,</strong> you wanted only a Banana but what you get is “A Gorilla holding a banana and the entire jungle”.</p><p>So inheritance makes a working copy of a class each time when you instantiate, irrespective of whether your object do need all those properties or not.</p><p><strong>Let’s come to the difference between Classical inheritance and Prototypal inheritance,</strong></p><p>As we discussed, what classical inheritance does is, it makes a deep copy of the classes for each object and have a rigid structure.</p><p>But prototypal inheritance can have the inheritance with out deep copy of all the properties( or shallow copy of some properties ) and also have a flexible structure.</p><h4><strong>Deep dive into prototypal inheritance</strong></h4><p>Actually there are three types of prototypal inheritance, they are</p><ol><li>Concatenative/Cloning,</li><li>Prototype delegation,</li><li>Functional inheritance</li></ol><p><strong>Concatenative/Cloning:</strong></p><p>The process of inheriting features directly from one object to another by copying the source objects properties. In JavaScript, source prototypes are commonly referred to as <strong>mixins.</strong></p><p><strong>Prototype delegation:</strong></p><p>This is the type of prototypal inheritance that is used in the javascript more. What it does is when an object is created from another object, instead of copying from that object, what it does is, it creates a linkage to the object from which it is created for <strong>delegation.</strong></p><blockquote>Here delegation means, consider you have a task to complete something, but for some reason you can’t do it. So what you could do in that case is, you can ask someone to do the work on behalf of you. <strong>You are delegating the task to someone.</strong></blockquote><p><strong>Functional inheritance:</strong></p><p>This type of inheritance is also observed in javascript. In javascript you can create an object with the help of functions, if you create an object without the constructor, the function is called factory functions. Functional inheritance works by producing an object from a factory, and extending the produced object by assigning properties to it directly (using concatenative inheritance).</p><p>Okay, now we know what is class based inheritance and prototypal based inheritance is.</p><p>Now before answering the question of the article, let’s see how javascript object is created and how they are linked.</p><p>For simplicity, let’s consider an object that is formed by a constructor function,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a83a3tSgMZuqlcsVCNup5w.png" /></figure><p>In the above snapshot, what I have done is, I created a constructor function called constructObj with two properties namely name and id, and one method named printDetails() to print the name and id. I printed the object b in console to know about the object.</p><p>Here you can see a __proto__ after the members of the object. When you expand the __proto__ object, we can see our constructor function as a value of the constructor property and also another __proto__. Under the nested __proto__ we can see the methods like toString(), hasOwnProperty() but the most important thing is constructor property holding Object() function as a value. If you expand the constructor having Object() as a value, you cannot find another __proto__. Because Object() __proto__ points to null.</p><blockquote><strong>Wait what the heck is __proto__?</strong></blockquote><p>__proto__ is the linkage that forms the “what we called inheritance” between the entities. Each object has __proto__ linking to some other object or in the simplest case( like objects created using factory functions ) pointing to the Object(). This is the linkage what generally called as a prototype chain. It’s a chain of __proto__ of different objects, that makes the delegation possible.</p><h3>Let’s dive into the actual topic of the article and how it relates to the stuff that we have discussed so far.</h3><p>Okay, now it’s the time to answer. Why I call it a delegation than an inheritance?<br>Beacause, consider the above snapshot, what if I execute a code like</p><blockquote>b.toString()</blockquote><p>it won’t throw error right? Yeah.</p><p>But what’s really happening behind the scenes is that, since we didn’t define any toString() method in the constructor function, the js engine lookup in the prototype chain for toString() method for delegation, in our case Object() has the toString() method. So it ask the Object() to execute the toString() on behalf of b object.</p><blockquote>Now we can see the picture of delegation of tasks through the prototype chain. This is why we can say prototypal inheritance of javascript is nothing but a delegation than inheritance itself.</blockquote><p>And it’s also important to consider what happens when we define toString() inside the constructor function? In this case when javascript starts to look in the object’s own methods, it will find the toString() method. So it will execute the toString() method defined in the constructor funtion. Actually this is what is called as shadowing the prototype in javascript. Because toString() method in the constructor function shadows the toString() method in the Object() function.</p><p>I know some people especially for someone who is used to classical inheritance would find this hard to wrap their head around. Because I have felt the same when I started to learn this for the first time. So it’s completely normal to feel confused but as you practice more and more, lookup more articles you will get the sense of it.</p><p>Drop a comment if you find this useful, have a good day.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=12d65dc0525d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[INTERSECTION OBSERVER API]]></title>
            <link>https://medium.com/@yknevenky/intersection-observer-api-21e82b2da7cc?source=rss-b30af7fe1d58------2</link>
            <guid isPermaLink="false">https://medium.com/p/21e82b2da7cc</guid>
            <dc:creator><![CDATA[Venky]]></dc:creator>
            <pubDate>Tue, 12 May 2020 16:35:38 GMT</pubDate>
            <atom:updated>2020-05-12T16:44:10.522Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cDyP9NKjl5JvGV9-ngWRYQ.png" /><figcaption>An abstract view of how Intersection Observer API works</figcaption></figure><p>Have you ever thought of coding a site, where you want to lazily load images or you wanted your video to get paused and resumed automatically when it is out of the view or back to the view?</p><p>Well maybe, you have thought of doing it with some third party javascript package, but guess what there is also vanilla way of doing it.</p><p><em>TADAAAA!! </em><strong><em>INTERSECTION OBSERVER API !!</em></strong></p><p>Ok, why you need IntersectionObserver API, when you have packages to do the job? The answer is dependency issue.</p><p>Let me tell you something first, I am not going to talk about how to implement lazy loading, auto pause and play using INTERSECTIONOBSERVER API, rather I would talk about the theory(kinda) of INTERSECTIONOBSERVER API and an abstract example of how to implement auto play and pause video.</p><p><strong>First of all what the heck is OBSERVER??</strong></p><blockquote>Observers are objects that observes something in real time and there are different observers to observe different things.</blockquote><p><strong>What does intersection mean here?</strong></p><blockquote>Well intersection is what it exactly sounds like, intersection of some elements.</blockquote><p>Then <strong>what is IntersectionObserver ?</strong></p><blockquote>An observer to observe whenever a <strong><em>Target element</em></strong>(element under observation) intersects with the <strong><em>Ancestor element.</em></strong></blockquote><p>Wait what the hell does that mean? Let me give you an example, imagine yourself standing in a railway overpass in a railway station, here you are an observer(aka ancestor element) standing inside the overpass, observing the railway tracks and the trains that are coming to the station. Here you are an observer, observing the railway track for trains, which are target elements</p><blockquote>To put it more simple,</blockquote><blockquote><strong>TRAINS =&gt; Elements/Nodes that needs to be observed</strong></blockquote><blockquote><strong>Tracks under the view of the observer =&gt; Window in which you observe/ Window through which you observe</strong></blockquote><blockquote><strong>YOU =&gt; Observer/Ancestor element</strong></blockquote><figure><img alt="Image credits: google" src="https://cdn-images-1.medium.com/max/1024/1*ozCfr9HGvtj-WAmgCRSJoQ.jpeg" /></figure><p>Ok, now with the code part,</p><pre>let observer = new IntersectionObserver(callback, options);</pre><p>First you have to instantiate an IntersectionObserver Object. The object needs two parameters, one is a callback which gets invoked whenever there is an intersection of the target element (in our railway overpass example, it gets invoked whenever the train comes into view of our window) and an options parameter(which is also an object) that configures how you want to detect, in how much of visibility and when to do the functionality defined by the callback.</p><p>So in short,</p><blockquote>The tasks like loading lazy images, pausing/resuming videos automatically, animations etc, are done by the call back. The functionalities that you want to execute when the target element is visible are defined in the callback.</blockquote><blockquote>The options object does the configuration goals of the observer</blockquote><h3>The callback</h3><p>The callback also takes two parameters, which are entries( you can imagine this as an array of trains that are coming into the intersection/window view) and other one is <strong>the observer object itself which you just instantiated with IntersectionObserver() constructor</strong>, it might be hard to wrap your head around it especially if you are a newbie to JS, but it is how it works, so just stick it to your skin, and follow.</p><pre>let observer = new IntersectionObserver(</pre><pre>(entries, observer) =&gt; { </pre><pre>  entries.forEach(entry =&gt; {</pre><pre><em>/* Here&#39;s where we deal with every train that we observe*/</em></pre><pre><br>  });<br>}, </pre><pre>options);</pre><h3>The options object</h3><p>The options object lets you to configure, what is the window through which you observe and based on how much you observe, when to fire the callback.</p><p>Going back to the railway overpass example, you might want to fire the callback as soon as the train comes into the view, or you might want to fire the callback only when the whole train or the last section of the train comes into view.</p><p>There are three properties that the options object take, if you don’t define them, each of them falls back to their defaults, the three properties are</p><ol><li><strong>root</strong></li><li><strong>rootMargin</strong></li><li><strong>threshold</strong></li></ol><p><strong><em>The root property:</em></strong></p><p>The ancestor element/observer, that the observed element will intersect. Think of it as whether you want to observe only one track or a window that comprises all the tracks. So it’s basically the <strong>Window through which you want to observe the target elements</strong>.</p><p><strong><em>The rootMargin property:</em></strong></p><p>The rootMargin property lets you adjust the size of the window.</p><p><strong><em>The threshold property:</em></strong></p><p>The threshold property enables you to decide when to fire the callback, for example if you want to fire the callback as soon as the first section of the train comes into the view of the window or only when half of the whole train comes into the view, or only when the last section comes into the view of the window(defined by rootMargin property).</p><pre>let options = {<br>    root: null,<br>    rootMargin: &quot;0px -250px&quot;,<br>              //Similiar to how you define margin property</pre><pre>    threshold: [0.3, 0.6]<br>  };</pre><p>In the above example, root has a value null, which means the window is the root element (which is also the default case), rootMargin defines the window size, here the -250px means you have to subtract 250px from both the left and right (like shrinking the window by a width of 250px from both sides).</p><p>Now you have set</p><ol><li>the window (set by the root property)</li><li>the size of the window (set by rootMargin property)</li><li>when to fire the callback based on how much of the target element comes into the view (set by threshold property)</li></ol><h3><strong>Now “putting it all together” part</strong></h3><p>Now you have defined the IntersectionObject, now what you need to do is that you ask the observer to start observing.</p><pre>let options = {<br>    root: null,<br>    rootMargin: &quot;0px&quot;,<br>    threshold: []<br>  };<br>let callback = (entries, observer)=&gt;{<br>  //The tasks that needs to be done by the callback when it is fired<br> };</pre><pre>let observer = new IntersectionObserver(handleIntersect, options);<br>observer.observe(targetElement);<br></pre><p>Hurray, now you have a good working template of IntersectionObserver API.</p><p>To concrete our understanding more, let’s look at an example of auto play/pause of video</p><pre>let video = document.querySelector(&#39;video&#39;);</pre><pre>let isPaused = false; <em>/* Flag for auto-paused video */</em></pre><pre><br>let observer = new IntersectionObserver(</pre><pre>  (entries, observer) =&gt; {</pre><pre>        entries.forEach(entry =&gt; {<br>            if(entry.intersectionRatio!=1  &amp;&amp; !video.paused){<br>                      video.pause();<br>                      isPaused = true;</pre><pre>              }  else if(isPaused) {<br>                     video.play();<br>                     isPaused=false<br>                 }<br>         });</pre><pre> }, {threshold: 1});</pre><pre><br> observer.observe(video);</pre><p>The above code is an example for how to auto pause and play based on the intersection of the target element with respect to ancestor element.</p><p>In <strong>entry.intersectionRatio!=1, intersectionRatio </strong>represents the ratio of the intersection of the target element with respect to the window size.</p><p>The value 1 means 100% of the video element is in the view, the value 0.6 means 60% of the video element is visible.</p><p>So it makes sense only to play the video when it is 100% covered by the window and also when it is not paused. And to pause the video, whenever the ratio is not 1 and the video is playing, that is what exactly the following code</p><pre>if(entry.intersectionRatio!=1  &amp;&amp; !video.paused){<br>                      video.pause();<br>                      isPaused = true;</pre><pre>}  else if(isPaused) {<br>                     video.play();<br>                     isPaused=false<br>}</pre><p>does.</p><p>And this is one of many use cases of IntersectionObserver API, the applications are many as long as the creativity never gets tired.</p><p>For more info about IntersectionObserver API, check out the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API">mozilla documentation</a>, because it’s always good to read the documentation.</p><p>And if you find this article helpful, drop a comment and let me know something interesting.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=21e82b2da7cc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>