<?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: Manish Champaneri</title>
    <description>The latest articles on DEV Community by Manish Champaneri (@mchampaneri).</description>
    <link>https://dev.to/mchampaneri</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%2F256861%2F3b1984ee-3163-433a-9aa3-c7872d794113.jpeg</url>
      <title>DEV Community: Manish Champaneri</title>
      <link>https://dev.to/mchampaneri</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mchampaneri"/>
    <language>en</language>
    <item>
      <title>Create calculator using mithril</title>
      <dc:creator>Manish Champaneri</dc:creator>
      <pubDate>Fri, 25 Oct 2019 05:44:38 +0000</pubDate>
      <link>https://dev.to/mchampaneri/create-calculator-using-mithril-5c84</link>
      <guid>https://dev.to/mchampaneri/create-calculator-using-mithril-5c84</guid>
      <description>&lt;p&gt;What you are building is this : &lt;a href="https://xp4xo.csb.app/"&gt;https://xp4xo.csb.app/&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Table of content
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Setup basic scaffold for calculator appliaction&lt;/li&gt;
&lt;li&gt;Mount mithril to the mounting point using m.mount&lt;/li&gt;
&lt;li&gt;Preapare UI for our calculator&lt;/li&gt;
&lt;li&gt;Create logic for arhitmatic operators &amp;amp; Inputs &lt;/li&gt;
&lt;li&gt;Live example hosted on codesandbox&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
   # Setup basic scaffold for calculator appliaction{#setup-basic-scaffold-for-calculator-application}
&lt;/h5&gt;

&lt;p&gt;Make HTML page having basic html tags and having mithril cdn within it. Also make sure to add a mounting point to the page which we will be using to inject our calculator code afterwards.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt; 
    &amp;lt;title&amp;gt; Calculator using mithril &amp;lt;/title&amp;gt;
    &amp;lt;script src="https://unpkg.com/mithril/mithril.js"&amp;gt;
    &amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
     &amp;lt;!-- Mounting point of calculator --&amp;gt;
     &amp;lt;div id='app'&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  # Mount mithril to the mounting point using m.mount {#mount-mithril-to-the-mounting-point-using-m-mount}
&lt;/h5&gt;

&lt;p&gt;We will be using m.mount function here to render our application.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;In hello world example we have used m.render function which is internally used by m.mount function. You can find comparision between m.mount vs m.render here.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Signature of m.mount function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;m.mount( mountingPoint , mithrilComponent )
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Parameter&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;mountingPoint&lt;/td&gt;
&lt;td&gt;Dom element where you want to mount mithril appliaction&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;mithrilComponent&lt;/td&gt;
&lt;td&gt;Mithril component you want to mount to the dom&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;p&gt;Mount mithril to &lt;strong&gt;div with id 'app'&lt;/strong&gt; using m.mount&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Mithril component is actully an object. An object which have mithril life-cycle methods and view method have defined in it.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var mountingPoint = document.getElementById('app')
m.mount ( mountigPoint, { 
  view() { 
    return m('Hello') 
  }
)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's assing mithril component to a variable and then pass it as paramter to make this code more readable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var mithrilComponent =  { 
  view() { 
    return m('Hello') 
  }
}
var mountingPoint = document.getElementById('app')
m.mount ( mountigPoint ,  mithrilComponent )
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
   # Preapare UI for our calculator{#prepare-ui-for-our-calculator}
&lt;/h5&gt;

&lt;p&gt;We have prepared mounting point and rended a component for verification. Now let's create basic UI for our preety simple calculator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var calculatorComp = {
    view() {
      return m("div", [
        m("table", [
          m("tr", [
             m("td",{
                  colspan: 4,
                  style: "width:100%;text-align:right; border:1px solid #ccc"
                },
                m("lable", { style: "width:100%,text-align:right" }, "0")
               )
            ]),
            m("tr", [
              m("td", m("button", "1")),
              m("td", m("button", "2")),
              m("td", m("button", "3")),
              m("td", m("button", "+"))
            ]),
            m("tr", [
              m("td", m("button", "4")),
              m("td", m("button", "5")),
              m("td", m("button", "6")),
              m("td", m("button", "-"))
            ]),
            m("tr", [
              m("td", m("button", "7")),
              m("td", m("button", "8")),
              m("td", m("button", "9")),
              m("td", m("button", "*"))
            ]),
            m("tr", [
             m("td", { rowspan: 2 }, m("button", "0")),
             m("td", m("button", "=")),
             m("td", m("button", "/"))
           ])
         ])
       ]);
    }
 };
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
   # Create logic for arhitmatic operators &amp;amp; input{#create-logic-for-arithmatic-operators-and-inputs}
&lt;/h5&gt;

&lt;p&gt;Now we need to define logic to process input according to operators. We will be using component's objects property to store our data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var calculatorComp = {
  previousInput: "0",
  currentInput: "0",
  currentResult: "0",
  previousOperator: "+",
  . . .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We will define to functions one to handle on click events of digit buttons and one for click event of operators&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;numberButtonClick(digit) {
  if (
    this.currentInput == 0 ||
    this.currentInput == this.currentResult
  ){
      this.currentInput = digit.toString();
    } else {
      this.currentInput = this.currentInput + digit.toString();
   }
},

operatorButtonClick(operator) {
  if (this.currentResult != this.currentInput) {
   this.previousInput = this.currentInput;
  } else {
    this.previousInput = 0;
  }

  switch (this.previousOperator) {
     case "+": {
     this.currentResult =
     parseInt(this.currentResult) + parseInt(this.previousInput);
     break;
   }
   case "-": {
      this.currentResult =
      parseInt(this.currentResult) - parseInt(this.previousInput);
      break;
   }
   case "*": {
      this.currentResult =
      parseInt(this.currentResult) * parseInt(this.previousInput);
      break;
   }
   case "/": {
      this.currentResult =
      parseInt(this.currentResult) / parseInt(this.previousInput);
      break;
  }
  default: {
      console.log(this.currentResult);
  }
 }
 this.currentInput = this.currentResult;
 this.previousOperator = operator;
},
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now only thing is left is to assign this methods to buttons we have created&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Assigning digit button click event
m("td",
   m("button",
       { onclick: e =&amp;gt; {
           e.preventDefault();
           this.numberButtonClick(1);
            m.redraw();
       }
     },"1"
   )
),

// Assigning operatorbutton click event
m("td",
   m("button",
       { onclick: e =&amp;gt; {
           e.preventDefault();
           this.operatorButtonClick("+");
            m.redraw();
       }
     },"1"
   )
),
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It would be too long if I write whole code here ;) , so instead see whole code from example hosted on codesandbox shown in next section.&lt;/p&gt;

&lt;h5&gt;
  
  
  # Live example hosted on codesandbox{#live-example-hosted-on-codesandbox}
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/embed/hello-world-example-xp4xo"&gt;https://codesandbox.io/embed/hello-world-example-xp4xo&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mithril</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Mithril Hello World Example</title>
      <dc:creator>Manish Champaneri</dc:creator>
      <pubDate>Thu, 24 Oct 2019 06:18:40 +0000</pubDate>
      <link>https://dev.to/mchampaneri/mithril-hello-world-example-4f99</link>
      <guid>https://dev.to/mchampaneri/mithril-hello-world-example-4f99</guid>
      <description>&lt;h5&gt;
  
  
  Table of content
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt; Introduction
&lt;/li&gt;
&lt;li&gt; Prepare basic HTML
&lt;/li&gt;
&lt;li&gt; Include Mithril  library
&lt;/li&gt;
&lt;li&gt; Add mounting element for Mithril in dom and mount Mithril there
&lt;/li&gt;
&lt;li&gt; Render "Hello world" by Injecting Hello world inside mounting element
&lt;/li&gt;
&lt;/ul&gt;




&lt;h5&gt;
  
  
  # Introduction
&lt;/h5&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Hello world&lt;/em&gt; program is best starting point to learn new programming language or framework. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This hello world program will explain how to do very basic minimal setup to use Mithril and then will end with displaying "Hello world" printed on screen.&lt;/p&gt;

&lt;h5&gt;
  
  
  # Prepare basic HTML
&lt;/h5&gt;

&lt;p&gt;Create html file with the basic tags of html, head, title, and body.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Mithril Hello World Example&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;   
   &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  # Include Mithril library
&lt;/h5&gt;

&lt;p&gt;We will be using Mithril CDN for our example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;head&amp;gt;
    &amp;lt;script src="https://unpkg.com/mithril/mithril.js"&amp;gt;
    &amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  # Add mounting element for Mithril in dom and mount Mithril there.
&lt;/h5&gt;

&lt;p&gt;We will be using div element with id &lt;em&gt;app&lt;/em&gt; as mounting point of our Mithril app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;body&amp;gt;
   &amp;lt;div id="app"&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we have to inform Mithril, that we want to use the element with id 'app' has to be used as mounting point of Mithril.&lt;br&gt;
In order to do that we have to select that dom element from dom.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; var app = document.getElementById("app") 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After adding mounting point our code will look like.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I am saying it as mounting point because mithril actually mounts dom elements to that dom element as child and you can have multiple mounting points for multiple mithril rendering functions. See example &lt;a href="http://learnit.mchampaneri.in/post/m-function-of-mithril#examples"&gt;multiple mounting points&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;body&amp;gt;
    &amp;lt;div id="app"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;script type="text/javascript"&amp;gt;
          var app = document.getElementById("app")
    &amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  # Render "Hello world" by Injecting Hello world inside mounting element
&lt;/h5&gt;

&lt;p&gt;We have added Mithril library and also have a mounting point for it. Let's render simple "Hello world" by injecting it inside mounting point.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Here &lt;em&gt;m&lt;/em&gt; represents Mithril object. &lt;br&gt;
We are using &lt;a href="http://learnit.mchampaneri.in/post/m-render-function"&gt;m.render&lt;/a&gt; function for rendering.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;Mithril Hello World Example&amp;lt;/title&amp;gt;
    &amp;lt;script src="https://unpkg.com/mithril/mithril.js"&amp;gt;&amp;lt;/script&amp;gt; 
    &amp;lt;/head&amp;gt;
    &amp;lt;body&amp;gt;
      &amp;lt;div id="app"&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;script type="text/javascript"&amp;gt;
          var app = document.getElementById("app");
          m.render(app, "Hello world");
        &amp;lt;/script&amp;gt;
    &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h5&gt;
  
  
  # Example
&lt;/h5&gt;

&lt;p&gt;Take a look at live example hosted on codesandbox !&lt;br&gt;
&lt;a href="https://codesandbox.io/embed/keen-mendel-li0c3"&gt;Codesandbox Example&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>mithriljs</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
