<?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 Pongpanot Tassaneeyachumphalee on Medium]]></title>
        <description><![CDATA[Stories by Pongpanot Tassaneeyachumphalee on Medium]]></description>
        <link>https://medium.com/@pongpanottassaneeyachumphalee?source=rss-a848b5f7c7ac------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*WPOuii-ePqxCRZ2HQCoBow.jpeg</url>
            <title>Stories by Pongpanot Tassaneeyachumphalee on Medium</title>
            <link>https://medium.com/@pongpanottassaneeyachumphalee?source=rss-a848b5f7c7ac------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 06:41:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pongpanottassaneeyachumphalee/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[อยาก Observer Property ใน lit-element ให้ได้แบบ Polymer จุง]]></title>
            <link>https://medium.com/@pongpanottassaneeyachumphalee/%E0%B8%AD%E0%B8%A2%E0%B8%B2%E0%B8%81-observer-property-%E0%B9%83%E0%B8%99-lit-element-%E0%B9%83%E0%B8%AB%E0%B9%89%E0%B9%84%E0%B8%94%E0%B9%89%E0%B9%81%E0%B8%9A%E0%B8%9A-polymer-%E0%B8%88%E0%B8%B8%E0%B8%87-695e35935a16?source=rss-a848b5f7c7ac------2</link>
            <guid isPermaLink="false">https://medium.com/p/695e35935a16</guid>
            <category><![CDATA[web-components]]></category>
            <category><![CDATA[litelement]]></category>
            <category><![CDATA[html]]></category>
            <category><![CDATA[polymer]]></category>
            <dc:creator><![CDATA[Pongpanot Tassaneeyachumphalee]]></dc:creator>
            <pubDate>Mon, 11 Jun 2018 15:14:31 GMT</pubDate>
            <atom:updated>2018-06-11T15:14:31.476Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*chegtGNOsK84SbBX15d9-Q.png" /><figcaption><a href="https://www.freepik.com/free-vector/businessmen-being-observed_1076104.htm"><strong>Designed by Freepik</strong></a></figcaption></figure><p>Observer นั้นคือการเฝ้ามอง Property ของ Element วาสมีการเปลี่ยนหรือไม่ หากเปลี่ยนแปลงให้ทำตาม function ที่เรากำหนดก็แค่นั้น</p><p>เท่าที่ผมรู้ใน Polymer สามารถ Observer Property ได้ 3 จุด คือ</p><pre># 1. ผ่าน static get properties()</pre><pre>static get properties(){<br>   return {<br>      <strong>prop1</strong>:{<br>        type: String<br>        observer: &#39;<strong>_doSomethingWhenProp1ChangedFunction</strong>&#39;<br>      }<br>   } <br>}</pre><pre># 2. ผ่าน static get observer()</pre><pre>static get observers(){<br>   return [&#39;<strong>_doSomethingWhenProp1ChangedFunction(prop1)</strong>&#39;]<br>}</pre><pre><br># 3. ผ่าน Template</pre><pre>html`<br>  &lt;div&gt; [[<strong>_doSomethingWhenProp1ChangedFunction(prop1)</strong>]] &lt;div&gt;<br>`</pre><p>แล้วใน lit-element ล่ะ ทำไง!! <br>ได้ 2 จุดดังนี้</p><pre># 1. ผ่าน disRender</pre><pre>_didRender(props, changedProps, prevProps){<br>    if(&#39;<strong>prop1</strong>&#39; in changedProps){<br>       <strong>this._doSomethingWhenProp1ChangedFunction(...)</strong><br>    }<br>}</pre><p>_didRender นั้นเป็น Lifecycle ของ lit-element ซึ่งมันจะทำงานทุกครั้งก็ต่อเมื่อ Template นั้นถูก Render (Template ในที่นี้หมายถึง <strong>_render()</strong>) ซึ่งTemplate จะทำงานทุกครั้งก็ต่อเมื่อ Property มีการเปลี่ยนแปลงค่า นั่นเอง</p><p>โดยเมื่อ _didRender ทำงาน จะรับ Argument มา 3 ตัว ได้แก่</p><ol><li>props =&gt; คืนค่า property ทั้งหมดที่มีอยู่ใน ELement นั้น <br>ตัวอย่างข้อมูล : { <strong>prop1</strong>:”newValue” , prop2:”hello”}</li><li>changedProps =&gt; คืนค่าเฉพาะ property ที่ถูกเปลี่ยนแปลงค่า พร้อมค่าใหม่<br>ตัวอย่างข้อมูล : { <strong>prop1</strong>:”newValue”}</li><li>prevProps =&gt; คืนค่าเฉพาะ property ที่ถูกเปลี่ยนแปลงค่า พร้อมค่าเก่า<br>ตัวอย่างข้อมูล : { <strong>prop1</strong>:”oldValue”}</li></ol><p>การทำ Observer ของ lit-element ก็แค่อาศัย Argument ของ changedProps มาเช็คว่าการ render ในครั้งนี้มีการคืนค่า <strong>prop1</strong> มาหรือเปล่า ถ้าคืน แสดงว่า Property <strong>prop1</strong> มีการเปลี่ยนแปลง หากมีการเปลี่ยนแปลงก็ให้ไปทำ <strong>_doSomethingWhenProp1ChangedFunction </strong>นั่นเองซี่งสามารถดูตัวอย่างตาม Code ในบรรทัดที่มี <strong>if</strong> ด้านบนนั่นเอง</p><pre># 2. ผ่าน shouldRender </pre><pre>_shouldRender(props, changedProps, prevProps){<br>    if(&#39;<strong>prop1</strong>&#39; in changedProps){<br>       <strong>this</strong>.<strong>_doSomethingWhenProp1ChangedFunction(...)</strong><br>    }<br>    return <strong>true</strong><br>}<br></pre><p><strong>วิธีนี้ไม่แนะนำให้ใช้</strong> แค่บอกว่ามันก็ทำได้เหมือนกับแบบที่ 1 แหละ แต่ถ้าใช้จะต้องมาคอย return ture เพื่อให้ template มัน render เองนะ</p><p># ทั้งสองวิธีอย่านี้อย่าลืมกำหนด Property ก่อนนะ หากไม่กำหนดมันจะไม่ทำงานเน้อ ตัวอย่างก็ตามนี้นะ</p><pre>static get properties() {<br>   return {<br>      <strong>prop1</strong>: String<br>   }<br>}</pre><p>สำหรับวันนี้ก็ง่วงนอนเช่นเคย วิกาลสวัสดิ์ นะครับ</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=695e35935a16" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[มาสร้าง One Page กับ lit-element กันเถอะ (ใช้ Lazyload ด้วยนะ)]]></title>
            <link>https://medium.com/@pongpanottassaneeyachumphalee/one-page-%E0%B8%81%E0%B8%B1%E0%B8%9A-lit-element-%E0%B8%81%E0%B8%B1%E0%B8%99%E0%B9%80%E0%B8%96%E0%B8%AD%E0%B8%B0-1753e3d1e18e?source=rss-a848b5f7c7ac------2</link>
            <guid isPermaLink="false">https://medium.com/p/1753e3d1e18e</guid>
            <category><![CDATA[sap]]></category>
            <category><![CDATA[polymer]]></category>
            <category><![CDATA[litelement]]></category>
            <category><![CDATA[web-components]]></category>
            <category><![CDATA[lazy-load]]></category>
            <dc:creator><![CDATA[Pongpanot Tassaneeyachumphalee]]></dc:creator>
            <pubDate>Sun, 03 Jun 2018 09:21:00 GMT</pubDate>
            <atom:updated>2018-06-05T02:20:14.749Z</atom:updated>
            <content:encoded><![CDATA[<p>จากบทความก่อนหน้า <a href="https://medium.com/@pongpanottassaneeyachumphalee/one-page-%E0%B8%87%E0%B9%88%E0%B8%B2%E0%B8%A2%E0%B9%86-style-pwa-helpers-8ca54bfbcfd8">(One Page ง่ายๆ Style PWA-Helpers)</a> ผมได้อธิบายการใช้งาน router.js ของ pwa-helper ทำ SPAs กับ HTML ธรรมดา ซึ่งก็มีความยุ่งยากพอสมควร</p><p>บทความนี้เราก็ยังวนเทียนอยู่กับการสร้าง One page เหมือนเดิม แต่เพิ่มเติมคือใช้ lit-element ในการช่วยสร้างให้มันง่ายขึ้น ซึ่งผมได้อธิบายไว้ในหัวข้อเรื่อง <a href="https://medium.com/@pongpanottassaneeyachumphalee/lit-element-%E0%B8%81%E0%B8%B1%E0%B8%9A%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%AA%E0%B8%A3%E0%B9%89%E0%B8%B2%E0%B8%87-custom-element-%E0%B9%81%E0%B8%9A%E0%B8%9A%E0%B8%AB%E0%B8%A7%E0%B8%B4%E0%B8%A7%E0%B9%86-e05ebea3fc9d"><strong>lit-element</strong> กับการสร้าง <strong>Custom Element </strong>แบบหวิวๆ</a> ก็ลองไปอ่านดูกันได้นะครับ</p><p>ลุยเลยละกัน!!</p><p><strong>ขั้นตอนที่ 1</strong></p><p>ให้เราลองมาดูโครงสร้าง Project ของเราที่จะทำกันก่อน โดยมีหน้าตาโครงสร้างแบบนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/370/1*Jf4koOBbdHyks5evcdkfJQ.png" /><figcaption>โครงสร้าง โปรเจค</figcaption></figure><p><strong>node_modules </strong>=&gt; Folder ที่เก็บโมดูลที่เราทำการ Install ไว้<br><strong>index.html</strong> =&gt; คือหน้าหลักของเว็บที่ทำการเรียก my-app.js มาแสดงผล<br><strong>my-app.js</strong> =&gt; คือส่วนการทำงานหลักที่ใช้ทำ SPAs ซึ่งจะถูกสร้างด้วย lit-element<br><strong>my-view1.js</strong> =&gt; คือข้อมูลที่แสดง ของหน้า 1<br><strong>my-view2.js</strong> =&gt; คือข้อมูลที่แสดง ของหน้า 2<br><strong>package-lock.json</strong> =&gt; จริงๆมันไม่เกี่ยวหรอกันคือ lock ต่างๆของ package ที่ทำการ install<br><strong>package.json</strong> =&gt; ส่วนที่เก็บรายละเอียดโปรเจคของเรา ซึ่งมันเป็นพื้นฐานของโปรเจคอยู่แล้ว</p><p><strong>ขั้นตอนที่ 2</strong></p><p>ทำการตั้งต้น (init) โปรเจคของเราให้เรียบร้อย โดยมีขั้นตอนดังนี้</p><p>2.1 สร้าง Folder โปรเจคของเรา จากนั้นเข้าไปที่ Folder แล้วป้อนคำสั่ง เพื่อสร้างไฟล์ package.json ให้พร้อมสำหรับการติดตั้ง Library ในขั้นตอนถัดไป</p><pre>$ npm init -y</pre><p>2.2 ติดตั้ง Library lit-element ที่เราจะนำมาช่วยสร้าง Custom Elements</p><pre>$ npm i @polymer/lit-element</pre><p>2.3 ติดตั้ง pwa-helper ซึ่งเป็นตัวช่วยสร้าง One Page ของเรานี่แหละ</p><pre>$ npm i pwa-helpers</pre><p>2.3 ติดตั้ง polymer cli เพื่อใช้ Static Server</p><pre>$ npm i polymer-cli --save-dev</pre><p>2.4 แก้ไขไฟล์ package.json โดยการ เพิ่มบรรทัดที่ 7 เข้าไป ซึ่งการเพิ่มนี้ทำให้เราเรียกใช้ Static Server ได้ง่ายขึ้นโดยเราจะเรียกใช้ในหัวข้อต่อๆไป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LMLC9OyE53wjEINJau52nA.png" /></figure><p><strong>ขั้นตอนที่ 3</strong></p><p>ลองสร้าง Hello World ซะหน่อย (โดยจะให้คำว่า hello myapp แสดงที่หน้าเว็บ)</p><p>เมื่อตั้งต้นโปรเจคของเราแล้ว ให้สร้างไฟล์ my-app.js ซึ่งเป็น Element หลักในการทำงาน โดยมีรายละเอียดขั้นตอนย่อยดังนี้</p><p>3.1 import ไฟล์ที่เกี่ยวข้อง มันก็คือไฟล์ที่เราเพิ่งจะ Install ไปในขั้นตอนที่ 2 นั่นแหละ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eLKGWhaBY2QWBAHSHgwGUg.png" /></figure><p>3.2 สร้าง class MyApp โดยมีการสืบทอด LitElement ที่เพิ่งได้ Import ไปมาใช้งาน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Hwbor825_qM6EYINuOODBA.png" /></figure><p>3.3 สร้าง Function _render ซึ่งเป็นส่วนของการ Return html ให้แสดงผลสู่หน้าเว็บ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cNcTVvhwHOY_pHQpjPPBDA.png" /></figure><p>3.4 บรรทัดสุดท้ายของไฟล์ให้เพิ่มคำสั่งการประกาศ Element ลงไป โดยการตั้งชื่อว่า my-app และอ้างอิง class Myapp มาใช้งาน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vo5qrBulfSvoDinhMCBAUQ.png" /></figure><p>3.5 จากนั้นให้เราสร้าง ไฟล์ html เพื่อเรียก ไฟล์ my-app.js มาแสดง โดยใน body ให้แสดงแท็กตามบรรทัดที่ 10,11 โดยมีความหมายดังนี้</p><p>บรรทัด 10 คือเรียก Element มาใช้งาน<br>บรรทัด 11 คือการ import ไฟล์ my-app.js</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PIGGF_QEQE2kcbk_VAiysQ.png" /></figure><p>3.6 เรียกคำสั่ง npm start ที่เราได้สร้างไปในหัวข้อ 2.4 เมื่อเรียกแล้ว polymer serve จะ Return url สำหรับทดสอบมา</p><pre>$ npm start</pre><pre>applications: <a href="http://127.0.0.1:8081/">http://127.0.0.1:8081</a><br>reusable components: <a href="http://127.0.0.1:8081/components/lit-route/">http://127.0.0.1:8081/components/lit-route/</a></pre><p>3.7 ทดสอบกับหน้าเว็บจะได้คำว่า hello myapp ที่คุมด้วย tag h1 ตัวใหญ่ๆ ออกมา</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*oxziHmRLGDdUCG2QEfTDWQ.png" /></figure><p><strong>ขั้นตอนที่ 4</strong></p><p>มาสร้างหน้าแต่ละหน้ากันครับ โดยเราจะสร้างแค่ 2 หน้าครับ</p><p>4.1 สร้างไฟล์ my-view1.js และ my-view2.js ประกอบด้วยโค้ดดังนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pH446FmAUPazonihIjmFNA.png" /></figure><p>ซึ่งโค้ดแต่ละบรรทัด ก็ได้อธิบายความหมายไปในขั้นตอนก่อนหน้าแล้ว ลองอ่านย้อนดูก่อนได้ครับ ว่ามีความหมายอย่างไร</p><p>โดยโค้ดด้านบนเป็นตัวอย่างของ my-view1 สังเกตุว่าจะ return คำว่า page1 ออกมาสู้หน้าเว็บ ส่วนไฟล์ my-view2 ลอกตามเลยครับ แต่แค่เปลี่ยนเลข 1 เป็น เลข 2</p><p>4.2 กลับมาที่ไฟล์ my-app.js ให้ทำการ import ไฟล์ที่เพิ่งได้สร้างไปครับ (ดูจาก บรรทัดที่ 4 และ 5) โดย import ต่อจากไฟล์ที่ได้ทำการเรียกไว้ครั้งที่แล้ว</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RJO6Ut0MWbioJaM1nzNMwQ.png" /></figure><p>4.3 จากนั้น ให้เรียก element โดยผ่าน tag html โดยมี tag main ครอบเป็น section เอาไว้ ตามโค้ดด้านล่าง บรรทัดที่ 12–15</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/804/1*xWJpOf92TccayIB-5oU6Ew.png" /></figure><p>4.4 บันทึก และทดสอบกับ browser ดู จะได้ผลลัพธ์ ตามนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AR7E9H4VtVMuqjMlTUqfLg.png" /></figure><p><strong>ขั้นตอนที่ 5</strong></p><p>จากขั้นตอนที่ 4.4 จะมีหน้าปรากฏทั้งสองหน้าคือ page 1 กับ page 2 แต่ในความเป็นจริงมันต้องควรแสดงแค่หน้าเดียวใช่ไหม เพราะฉนั้นเราจะมาสร้าง styleให้แสดงผลเพียงแค่หน้าเดียวกัน</p><p>5.1 ในไฟล์ my-app.js ให้แทรก Tag style พร้อม code css ดังนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-8XunnbYVL_QRtSmhHzqgA.png" /></figure><p>code css ชุดนี้หมายความว่า จะซ่อน element ที่อยู่ใน tag main ไม่ให้แสดงออกในหน้าเว็บ ซึ่งผลลัพธ์จะออกมาตามแบบรูปด้านล่างนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7WE7DQpwOcfBq8aJX6C6wA.png" /></figure><p>จะเห็นว่า my-view1 my-view2 ที่เราเอาออกมาแสดงหายไปแล้ว แต่จริงๆ มันควรจะ active 1 หน้า</p><p>5.2 ดังนั้น ในบรรทัดที่ 17 ตามรูปด้านล่าง เราจึงต้องเพิ่ม code css เข้าไป จากนั้นในบรรทัดที่ 25 ให้เพิ่ม attribute active เข้าไป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mtJEaWghqXNIVAV90tYefQ.png" /></figure><p>คำสั่งข้างต้นนั้น หมายความว่า หาก element ที่อยู่ใน tag main มี attribute active อยู่จะทำให้ element ที่ถูกซ่อนไป แสดงออกมาตามรูปด้านล่างนั่นเอง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/802/1*vWQ6hWuscJAcyX-wHp5hoQ.png" /></figure><p>แต่หากจะนำไปใช้งานจริงเพียงแค่นี้มันยังใช้ไม่ได้ เราจะต้องควบคุมการแสดงผลของ my-view1 และ my-view2 ผ่าน Property ใน my-app.js ดังขั้นตอนด้านล่าง</p><p><strong>ขั้นตอนที่ 6</strong></p><p>เริ่มทำการกำหนด Property</p><p>6.1 ให้กำหนด Property ชื่อว่า _page และเป็น string ใน element my-app</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v8KlCaeWrLxwucbj-Etppw.png" /></figure><p>6.2 กำหนดค่าเริ่มต้นให้ Property _page โดยการกำหนดใน constructor</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J0gRqCDQ6BMstfFer0xLww.png" /></figure><p>6.3 ใน function _render ให้รับ args props มา จากนั้น เพิ่มคำสั่ง javascript ลงใน element my-view1 และ my-view2</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DSpQCf2iL4zCE5VNGulOqQ.png" /></figure><p>จากรูปด้านบนหมายความว่า</p><p>6.3.1 หากกำหนด Property _page = ‘view1’ มันจะ เพิ่ม attribute active ให้กับ element my-view1 นั่นหมายความว่า page1 จะแสดงออกสู่หน้าเว็บ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1010/1*bMe8Bn5WMjYY5ubdl2zW7w.png" /></figure><p>6.3.2 หากกำหนด Property _page = ‘view2’ มันจะ เพิ่ม attribute active ให้กับ element my-view2 นั่นหมายความว่า page2 จะแสดงออกสู่หน้าเว็บ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/1*tRsNy9XYhfZPfFPbAcDz9Q.png" /></figure><p>แต่เราจะมากำหนด static property ดังขั้นตอนที่ 6.2 บรรทัดที่ 18 ไม่ได้ ฉนั้นเราต้องให้ property _page มีค่า dynamic ตาม url ที่ป้อน ดั่งขั้นตอนที่ 7</p><p><strong>ขั้นตอนที่ 7</strong></p><p>เราต้องทำ property _page ให้เปลี่ยนค่าตาม url ที่เราป้อนเช่น</p><p>127.0.0.1:8081/view1 =&gt; property _page จะมีค่าเท่ากับ /view1<br>127.0.0.1:8081/view2 =&gt; property _page จะมีค่าเท่ากับ /view2</p><p>7.1 นำ pwa-helper ที่เราเพิ่งได้ import ในขั้นตอนที่ 3.1 มาช่วย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8XPGdOrUFgzy8XjEZdG2Og.png" /></figure><p>โดยคำสั่งจะอยู่ภายใต้ Function _firstRendered ซึ่ง Function นี้จะทำงานหลังจาก Function _render ทำหาร render html ออกมาสู่หน้าเว็บ</p><p>เมื่อหน้าเว็บทำงาน Fuction installRouter จะทำงานและ Return Path ไปให้ _pageChange ทำงานต่อไป</p><p>7.2 สร้าง Function _pageChange รับ arg path มา จากนั้นทำการกำหนดค่าให้ property _page</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eBpDNJwUBIkqPqgoAoQP_g.png" /></figure><p>7.3 ทดลองกับเว็บ Browser</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1022/1*u7ha1umwwU6luWWMZ6lSig.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*u9n0TjAvPJtzqPtX1Bd5TA.png" /></figure><p>จะเห็นได้ว่า</p><p>หากเราใช้ url : 127.0.0.1:8081/view1 =&gt; จะแสดง page 1<br>หากเราใช้ url : 127.0.0.1:8081/view2 =&gt; จะแสดง page 2</p><p>7.4 แต่หากเราให้ url : 127.0.0.1:8081 =&gt; มันจะไม่แสดงอะไรเลยเนื่องจากมันไม่ได้มีคำครองจองกัน ฉนั้นใน Function _pageChange เราต้องมีการแก้ไขโค้ดเล็กน้อยดังนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gQs4FZv3QHt2CSnecw6RQQ.png" /></figure><p>โค้ดด้านบนนี้หมายความว่า หาก path ที่ได้เป็นแค่ “/” ให้แทนที่ด้วย “/view1” จากนั้นลองทดสอบดูจะให้ว่าใช้งานได้ แม้ไม่ต้องเพิ่ม Path “/view1”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*qITz6IRDwFTNh-1l0w4mcg.png" /></figure><p><strong>ขั้นตอนที่ 8</strong></p><p>สร้าง เมนูให้คลิกได้ซักนิด โดยการเพิ่มในส่วนของ tag manu ลงไปในไฟล์ my-app.js</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0YniYhEYE2I_M_9qWjXq3Q.png" /></figure><p>จากนั้นลองทดสอบดูจะเห็นได้ว่าเราคลิกเปลี่ยนหน้าได้สพดวกโยธินกันเลยทีเดียว</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mcFU9vvrRYbirQ5WqiCsVg.png" /></figure><p>แค่นี้ก็เสร็จแล้ว One Page ของเรา <br>แต่!!! หากเราเรียกแค่หน้า view1 มันก็ควรที่จะโหลดไฟล์แค่ my-view1.js ใช่ไหม แต่จากรูปที่ 4.2 เรา import มาทั้ง my-view1.js และ my-view2.js เลย <br>ฉนั้นเรานำหลักการ Lazyload มาให้ในขั้นตอนที่ 9 กันเถอะ</p><p><strong>ขั้นตอนที่ 9</strong></p><p>ทำ Lazyload กันเถอะ<br>ถ้าแปลภาษาชาวบ้านๆก็คือ เรียกลิงค์หน้าไหนก็โหลดหน้านั้น ไม่ต้องโหลดมาทั้งหมด….</p><p>9.1 เปลี่ยน Function _pageChange ให้เป็น async ซะ จากนั้นเพิ่มบรรทัด await ลงไปโดยให้ไป import my-view(path ที่ได้รับโดยตัด “/” ออก).js เพื่อทำการ Dynamic import ตามหน้านั่นเอง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KywRwpAbiT1hkjBuQgUYNg.png" /></figure><p>9.2 ตัดการ import ของบรรทัดที่ 4 กับ 5 ทิ้งไปเลยยยย</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Kvj0KWSZt5Jsw0BBProyOQ.png" /></figure><p>9.3 ทดลองดูซิ แค่นี้ก็ใช้ได้แล้ว โดยที่เราไม่ต้อง import บรรทัด 4-5 ตามรูปด้านบนอีก โดยมันจะ Import ก็ต่อเมื่อมันถูกเรียกใช้งานผ่าน url เท่านั้นๆๆๆๆ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hcXX8fuOGrQQXs_Wr2871g.png" /></figure><h3><strong>จบปิ้ง..</strong></h3><p>เป็นยังไงกันบ้างครับ รู้เรื่องกันบ้างไหม 555 ผมก็สอนแบบงงๆไปแหละครับ จริงที่สอนคือก็แค่ให้เข้าใจหลักการทำงานเฉยๆแหละ <strong>ไม่ได้เน้นความถูกต้องอะไรมากมาย </strong>ยังไงถ้าผิดพลาดตรงไหนก็แจ้งได้นะครับ</p><p>สำหรับวันนี้ขอไปนอนก่อน <strong>สวัสดีครัชชชช..</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1753e3d1e18e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[lit-element กับการสร้าง Custom Element แบบหวิวๆ]]></title>
            <link>https://medium.com/@pongpanottassaneeyachumphalee/lit-element-%E0%B8%81%E0%B8%B1%E0%B8%9A%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%AA%E0%B8%A3%E0%B9%89%E0%B8%B2%E0%B8%87-custom-element-%E0%B9%81%E0%B8%9A%E0%B8%9A%E0%B8%AB%E0%B8%A7%E0%B8%B4%E0%B8%A7%E0%B9%86-e05ebea3fc9d?source=rss-a848b5f7c7ac------2</link>
            <guid isPermaLink="false">https://medium.com/p/e05ebea3fc9d</guid>
            <dc:creator><![CDATA[Pongpanot Tassaneeyachumphalee]]></dc:creator>
            <pubDate>Mon, 21 May 2018 15:33:21 GMT</pubDate>
            <atom:updated>2018-05-21T15:33:21.887Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>lit-element</strong> กับการสร้าง <strong>Custom Element </strong>แบบหวิวๆ</p><p><strong>lit-element </strong>คือ library ที่ใช้สร้าง Custom Element แบบง่ายๆตัวหนึ่งซึ่งเบาหวิวๆ ตามชื่อหัวข้อนั่นแหละ ซึ่งถูกสร้างโดยทีมงาน <strong>Google Polymer</strong> แต่มันใช้ง่านกว่า Polymer ซะอีก</p><p>หากใครไม่รู้จักว่า Custom Element คืออะไรก็อ่านตามลิงค์นี้ดูนะ <a href="https://medium.com/@tanakritsai/web-components-b20301f1cd33">https://medium.com/@tanakritsai/web-components-b20301f1cd33</a></p><p>สรุปง่ายๆคือบทความนี้เราจะเรียก Element แบบนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nIDB7SMT5pzBms6nbZ8M4A.png" /></figure><p>ผลลัพธ์ก็จะออกมาแบบนี้ เป็น Card อันสวยงาม โดยใช้เจ้าตัว lit-element นี่แหละสร้างขั้นมา</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/1*DzmAFPC9HmPOn_MctcAjGQ.png" /></figure><p>มาดูกันเลยว่าเขียนยังไงบ้าง</p><ol><li>ติดตั้ง lit-element ให้กับโปรเจคเราซะ</li></ol><pre>npm i @polymer/lit-element</pre><p>2. สร้างไฟล์ my-card.js ซึ่งเป็นที่จะเก็บ Custom Element โดยมีลำดับขั้นตอนดังนี้</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/91107933ea7fec4a5693bf12e20631a0/href">https://medium.com/media/91107933ea7fec4a5693bf12e20631a0/href</a></iframe><p>3. สร้างไฟล์ index.html สำหรับเรียก Custom Element มาใช้งาน โดยมีลำดับขั้นตอนดังนี้</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36d361ae0457810ff566ccb04e46886b/href">https://medium.com/media/36d361ae0457810ff566ccb04e46886b/href</a></iframe><p>เสร็จแล้ว ทดสอบดูกับ Static Server ดูครับ ( ในที่นี้ผมใช้ polymer serve ) จะได้ผลลัพธ์ตามตัวอย่างที่เป็น Card อย่างสวยงาม</p><p>หากใครสนใจอยากได้ความรู้เพิ่มเติม สามารถเข้าไปอ่านได้ที่ <a href="https://github.com/Polymer/lit-element">https://github.com/Polymer/lit-element</a> นะครับ</p><p>!! สำหรับวันนี้ผม<strong>ง่วงนอน</strong>แล้วครับ ฝันดีครับผม</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e05ebea3fc9d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[One Page ง่ายๆ Style PWA-Helpers]]></title>
            <link>https://medium.com/@pongpanottassaneeyachumphalee/one-page-%E0%B8%87%E0%B9%88%E0%B8%B2%E0%B8%A2%E0%B9%86-style-pwa-helpers-8ca54bfbcfd8?source=rss-a848b5f7c7ac------2</link>
            <guid isPermaLink="false">https://medium.com/p/8ca54bfbcfd8</guid>
            <dc:creator><![CDATA[Pongpanot Tassaneeyachumphalee]]></dc:creator>
            <pubDate>Fri, 18 May 2018 18:02:27 GMT</pubDate>
            <atom:updated>2018-05-18T18:03:29.248Z</atom:updated>
            <content:encoded><![CDATA[<p>ในยุคนี้ทุกคนคงรู้จักเรื่องของ (SPAs) Single-Page Applications กันหมดแล้วเน้อ อธิบายง่ายๆก็แค่เปลี่ยนหน้าโดยไม่ต้อง Refresh หน้านั่นแหละ</p><p>ในที่สุดทางทีมงาน Polymer ได้สร้างเครื่องมือที่ชื่อว่า <a href="https://github.com/Polymer/pwa-helpers#routerjs">pwa-helpers /</a><a href="https://github.com/Polymer/pwa-helpers#routerjs">router.js</a>มาให้เราได้ใช้กัน รอช้าอยู่ใยไปกันเลย</p><pre><strong>1.ติดตั้งเครื่องมือลงไปก่อน </strong><br>$ yarn add pwa-helpers</pre><p>2. สร้างไฟล์ index.html</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2bdd4e7e252c111041fd91d586b7d959/href">https://medium.com/media/2bdd4e7e252c111041fd91d586b7d959/href</a></iframe><pre>ภายในไฟล์ประกอบไปด้วย<br>1. import ไฟล์ app.js แบบ module (จะสร้างไฟลนี้ในขั้นตอนถัดไป)<br>2. สร้าง style โดยให้ tag ที่มี class page ไม่ปรากฏบนหน้าจอ แต่หาก tag นั้นมี attribute active ก็ให้แสดง tag นั้นบนหน้าจอซะ<br>3. สร้างเมนูสำหรับเปลี่ยนหน้า ก็แค่คลิกลิ้งค์เปลี่ยนหน้านั่นแหละ<br>4. สร้างส่วนของหน้า ซึ่งประกอบด้วย 2 หน้า คือ view1 กับ view2</pre><p>3. สร้างไฟล์ app.js</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d24a0dc2bc80dedc64d40c492a598178/href">https://medium.com/media/d24a0dc2bc80dedc64d40c492a598178/href</a></iframe><pre>ภายในไฟล์นี้ประกอบไปด้วย <br>1. import เครื่องมือมาใช้<br>2. เรียกฟังชั้น installRouter มาใช้งาน โดย callback จะ return args คืนมาเป็น location ซึ่ง callback นี้จะทำงานก็ต่อเมื่อเปลี่ยนหน้า (ก็คือคลิกลิ้งที่เมนูที่เราเพิ่งสร้างไปที่หน้า index.html นั้นแหละ)<br>3. เมื่อเกิด event เปลี่ยนหน้า มันจะดึงหน้ามาเทียบทีละอันว่าตรงกับลิ้งที่เรากดไหม ถ้าหน้าไหนตรงจะ เพิ่ม attribute active ให้กับ tag นั้น แต่ถ้าไม่ตรงจะลบ attribute active ออก นั่นเอง</pre><pre><strong>4.start static server (ในที่นี้ใช้ Polymer serve)</strong><br>$ polymer serve</pre><p>ทดลองทดสอบกับ Browser ดูจะเห็นว่าเราสามารถเปลี่ยนหน้าแบบ (SPAs) ง่ายดายเลยทีเดียวเชียวล่ะ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/774/1*dYrMIKQ8TnliU_y6xwj49g.png" /></figure><p>นี่เป็นเพียงหลักการ ง่ายๆ ใครจะเอาไปประยุกต์ใช้ ยังไงก็แล้วแต่เด้อ</p><p>ง่วง!! แล้วครับฝันดีนะ</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8ca54bfbcfd8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Polymer data binding — (EP2) Binding to Object]]></title>
            <link>https://medium.com/@pongpanottassaneeyachumphalee/polymer-data-binding-ep2-binding-to-object-4c99cede619a?source=rss-a848b5f7c7ac------2</link>
            <guid isPermaLink="false">https://medium.com/p/4c99cede619a</guid>
            <category><![CDATA[data-binding]]></category>
            <category><![CDATA[html5]]></category>
            <category><![CDATA[web-components]]></category>
            <category><![CDATA[polymer]]></category>
            <dc:creator><![CDATA[Pongpanot Tassaneeyachumphalee]]></dc:creator>
            <pubDate>Sun, 30 Jul 2017 03:24:31 GMT</pubDate>
            <atom:updated>2018-05-18T16:19:00.165Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3SucKKmwW-fPfWtsYVKbgA.png" /><figcaption>Designed by Freepik</figcaption></figure><p>สวัสดีครับ EP2 นี่ ทิ้งห่างกับ EP1 มากๆ สงสัยคุณผู้อ่านคงลืม EP1 ไปแล้ว 555 ซึ่งในบทนี้ผมจะมาอธิบายเรื่อง การผูกกันของข้อมูลที่เป็น Object</p><p>เรามาดูตัวอย่าง Code กันเลยดีกว่า</p><pre>&lt;div&gt;<br>   [[user.name]]<br>&lt;/div&gt;</pre><pre>///////////////////////////</pre><pre>static get properties() {<br>   return { <br>      user:{<br>         value:{ name:&#39;Namkhaeng&#39;, age:18 }<br>      }<br>   };<br>}</pre><p>จากโค้ดด้านบนผลลัพธ์จะออกมาแบบนี้ครับ</p><pre>Namkhaeng</pre><p>ทีนี้ผมต้องการเปลี่ยนชื่อจาก Namkhaeng เป็น Pongpanot ผมก็แค่สร้างปุ่มไว้อันนึง ให้เรียกฟังก์ชันเปลี่ยนชื่อมา โดยมีโค้ดดังนี้</p><pre>&lt;div&gt;<br>   [[user.name]]<br>&lt;/div&gt;<br><strong>&lt;button on-click=&quot;changeName&quot;&gt;ปุ่ม change&lt;/button&gt;</strong></pre><pre>///////////////////////////</pre><pre>static get properties() {<br>   return { <br>      user:{<br>         value:{ name:&#39;Namkhaeng&#39;, age:18 }<br>      }<br>   };<br>}</pre><pre><strong>changeName(){<br>   this.user.name = &#39;Pongpanot&#39;<br>}</strong></pre><p>แต่เมื่อเราลองกดปุ่ม change ปรากฏว่าข้อความคำว่า Namkhaeng ยังไม่เปลี่ยนเป็น คำว่า Pongpanot ครับ **เอาแล้วๆ ทำไมไม่เปลี่ยนวะเนี้ย</p><p>จากโค้ดด้านบนเมื่อกดปุ่ม ผลลัพธ์จะออกมาเหมือนเดิม</p><pre>Namkhaeng</pre><pre>(ปุ่ม change)</pre><p>แต่ถ้าหากเรากำหนดแบบนี้ ในฟังก์ชัน changeName แทนละ</p><pre>changeName(){<br>   <strong>this.user = { name:&#39;Pongpanot&#39;, age:18 }</strong><br>}</pre><p>จากโค้ดด้านบนเมื่อกดปุ่ม change ผลลัพธ์จะออกมาแบบนี้</p><pre>Pongpanot</pre><pre>(ปุ่ม change)</pre><p>ปรากฏว่าข้อความเปลี่ยนเป็น Pongpanot แล้ว เย้ๆ อ้าวแล้วทำใมต้องกำหนดใหม่ด้วย ไม่มีอย่างอื่นเหรอ ทำใมมันถึงเป็นอย่างงั้นนะ</p><p>จากตอนที่แล้วถ้ายังจำได้ เมื่อเรากำหนด Property. Polymer จะทำการสร้าง Property Effects แล้วก็จะสร้าง Setter ของ Property นั้น และเมื่อจะกำหนดโค้ดแบบด้านล่างนี้ Setter ของ Property นั้นจะถูกเรียก ต่อด้วย Property Effects จะถูกเรียกต่อ จึงทำให้ข้อความถูกเปลี่ยนนั่นเอง</p><pre>this.user = { name:&#39;Pongpanot&#39;, age:18 }<br>//กำหนดแบบนี้ Setter ของ Propety ที่ชื่อว่า name จะถูกเรียก</pre><p>แต่ถ้าเรากำหนดแบบ Sub Property แบบด้านล่างนี้ Setter ก็จะไม่มีเหตุผลที่จะถูกเรียกใช้งาน และ Property Effect ก็จะไม่ทำงาน ทำให้ข้อมูลไม่เปลี่ยนบนหน้าจอนั่นเอง แต่ข้อมูลใน Property ถูกเปลี่ยนไปแล้วนะ แต่ว่าตอนแสดงผลไม่เปลี่ยนเฉยๆ</p><pre>this.user.name = &#39;Pongpanot&#39;<br>//กำหนดแบบนี้ Setter จะไม่ถูกเรียก</pre><p>แล้วถ้าจะทำให้ข้อมูลเปลี่ยนโดยการกำหนด Sub Property ไปเลยเราจะทำอย่างไรละ</p><p>ต้องอธิบายก่อนว่าเรากำหนด [[user.name]] แบบนี้ใน Template มันจะถูกเก็บไว้ในบัญชี Property Effects มันจะเปลี่ยนก็ต่อเมื่อมันถูกแจ้งว่าให้เปลี่ยน แต่มันดันไม่ถูกเรียกเพราะว่ามันไม่เข้า Setter เพราะฉนั้นเราก็แจ้งให้เปลี่ยนตรงๆไปเลยโดยการกำหนดแบบนี้</p><pre>changeName(){<br>   <strong>this.user.name = &#39;Pongpanot&#39;<br>   this.notifyPath(&#39;user.name&#39;,this.user.name)</strong><br>}</pre><p>จากโค้ดด้านบนเมื่อกดปุ่ม change ผลลัพธ์จะออกมาแบบนี้</p><pre>Pongpanot</pre><pre>(ปุ่ม change)</pre><p>สาเหตุที่มันเปลี่ยนเพราะว่า เราก็แค่บอกว่า Path [[user.name]] นี้มีการเปลี่ยนแล้วนะ จากนั้น Polymer มันก็จะไปดูที่ Property Effects ว่าเจ้า [[user.name]] อยู่ที่ไหน จากนั้นก็ไปเปลี่ยนข้อความที่นั่นซะ</p><p><strong>โค้ดแบบด้านบนมันยาวไปมีแบบสั้นกว่านี้ไหม?</strong><br>มีครับ Polymer มีให้คุณใช้หลายทางเลือก กำหนดแบบนี้ไปเลยเป็นไง</p><pre>changeName(){<strong><br>   this.set(&#39;user.name&#39;,&#39;Pongpanot&#39;)</strong><br>}</pre><p><strong>ทำใม!!! Polymer ถึงทำแบบนี้?</strong> ก็นะ..<br>ลองจินตนาการว่า Property ที่เป็น Object มี Sub Property เป็นพันๆดูซิ…. ช้าแน่นอน คือเราแค่เปลี่ยนไม่กี่จุด เราก็ควรบอกแค่เฉพาะจุดนั้น จะให้มันมาวิ่งดูทั้งหมดก็เสียเวลาเปล่าๆ</p><p>จบแล้วคร๊าบบบบบ . ถ้าหากมีอะไรสังสัยก็มาถามได้นะครับ</p><p>สำหรับวันนี้ผมง่วงนอนแล้ว…. ฝันดีครับ…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c99cede619a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Polymer data binding — (EP1) Data binding Under the hood]]></title>
            <link>https://medium.com/@pongpanottassaneeyachumphalee/polymer-data-binding-ep1-data-binding-under-the-hood-6e356cf4bf1c?source=rss-a848b5f7c7ac------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e356cf4bf1c</guid>
            <dc:creator><![CDATA[Pongpanot Tassaneeyachumphalee]]></dc:creator>
            <pubDate>Mon, 19 Jun 2017 10:43:12 GMT</pubDate>
            <atom:updated>2017-06-19T10:43:12.972Z</atom:updated>
            <content:encoded><![CDATA[<p>สวัสดีท่านผู้อ่านอีกครั้งนะครับ สำหรับวันนี้เราจะมาคุยเรื่อง Databinding system ใน Polymer กันครับผม</p><p>Data binding คืออะไร มันก็คือการเชื่อมต่อข้อมูลภายใน Custom element ไปยัง Property หรือ Attribute ต่างๆ หรือแม้กระทังส่งข้อมูลจาก Property จาก H<em>ost element (ผมจะเรียกว่าแม่) </em>ไปถึง Property ของลูก T<em>arget element (ผมจะเรียกว่าลูก) </em>ซึ่งข้อมูลจากลูกก็ขึ้นไปหาแม่เหมือนกัน ซึ่งคำว่า<em> </em>Data binding ในบทความนี้ผมขอใช้คำว่า การ(ผูกกัน)ของข้อมูลละกันนะครับ</p><pre>&lt;template&gt;<br>   &lt;div&gt; <strong>[[foo]]</strong> &lt;/div&gt;<br>   &lt;my-input value=&quot;<strong>{{bar}}</strong>&quot;&gt; &lt;/my-input&gt;<br>&lt;/template&gt;</pre><p>จากโค้ดด้านบนเราจะเห็นได้ว่า สิ่งที่บ่งบอกการผูกของข้อมูลของเราคือ [[ ]] <em>Square brackets anotation และ {{ }} Curly brackets anotation โดนความแตกต่างของทั้ง 2 ตัว มีดังนี้</em></p><p>[[ ]] เครื่องหมายแบบนี้หมายความว่าข้อมูลจะสามารถวิ่งจากแม่ไปลูกได้ แต่ว่ามันจะไม่วิ่งจากลูกไปแม่นะ (host to target)</p><p>{{ }} เครื่องหมายแบบนี้หมายความว่าข้อมูลจะสามารถวิ่งจากแม่ไปลูกได้ และสามารถวิ่งย้อนกลับจากลูกไปแม่ได้เช่นกัน (host to target) and (target to host)</p><p><strong>Under the hood</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/526/1*DpEQLYOzYGG7yfux-JfroA.png" /><figcaption><a href="http://www.freepik.com/free-vector/famous-story-characters-pack_849315.htm">Designed by Freepik</a></figcaption></figure><p>ต่อมาเราจะมาพูดถึงเรื่อง Under the hood กัน ว่าระบบ Data binding ใน Polymer ทำงานกันอย่างไร โดยใช้โค้ดชุดเดิมนี่แหละ</p><pre>&lt;template&gt;<br>   &lt;div&gt; <strong>[[foo]]</strong> &lt;/div&gt;<br>   &lt;my-input value=&quot;<strong>{{bar}}</strong>&quot;&gt; &lt;/my-input&gt;<br>&lt;template&gt;</pre><p>จากโค้ดเราได้กำหนด [[foo]] ไว้ที่ &lt;div&gt; ภายใน Template เมื่อ Custom element ถูกเรียกใช้งาน Polymer จะทำกา<em>ร </em>parse template หาดูว่ามีจุดไหนที่ทำการผูกข้อมูลไว้บ้าง <em>(ในที่นี้มี &lt;div&gt; กับ &lt;</em>my-input<em>&gt;)</em> แล้วมันจะเอาไปเก็บไปใน Array ที่ชื่อว่า Property effects โดยมีการจำลองว่ามันจะเก็บประมาณนี้นะครับ</p><pre>[<br>   {<br>     foo:[<br>        {<br>          effect : {<br>             index: 0,<br>             kind: &quot;text&quot;,<br>             mode: &quot;[&quot;,<br>             model: &quot;foo&quot;,<br>             signature: undefined,<br>             value: &quot;foo&quot;<br>          },<br>          kind:&quot;annotation&quot;<br>        }<br>     ],<br>     bar:Array...<br>   }<br>]</pre><p>ดูอีกแบบง่ายกว่า</p><pre>[<br>   { &quot;นำค่าของ foo มาหยอดที่ div&quot; },<br>   { &quot;นำค่าของ bar มาหยอดที่ my-input&quot; }<br>]</pre><p>นอกจากมันจะสร้างรายการใน Property effects แล้ว มันยังไปสร้าง setter เพื่อให้สามารถกำหนดค่าของ foo ได้ โดยโค้ดภายในจะทำการ Dirty checking ก่อนที่จะนำค่าไปเก็บด้วย โดยมีการจำลองว่าโค้ดจะเขียนประมาณนี้ครับ</p><pre><strong>set foo(val){</strong><br>   var old = this.__data.foo;<br>   if(old !== val){<br>      <strong>this.__data.foo = val;</strong><br>   }<br><strong>}</strong></pre><p>แล้ว Dirty checking คืออะไรล่ะ มันก็คือการเช็คค่าแบบแบบผิวๆว่า ค่าที่ถูกแก้ไขเป็นค่าเดิมไหม ถ้าค่าเดิมก็ให้ทำการเก็บค่าเข้าไปเท่านั่นเองครับ เน้นนะครับว่าผิวๆ ถ้าเราอัปเดตข้อมูลแบบ Array หรือแบบ Object มันจะใช้ไม่ได้นะเออ !!</p><p>สรุปซักท่อนก่อนนะ เมื่อเรา สร้าง Annotation มันจะไปเก็บประวัติว่าใครผูกข้อมูลอยู่ที่ property effects และไปสร้าง Setter ประจำ Property เพื่อรอใช้งาน</p><p>เมื่อเราแก้ไขค่า Property foo ตามโค้ดด้านล่าง</p><pre>this.foo = <strong>&quot;Hi&quot;</strong></pre><p>มันก็จะทำการ Dirty checking หากเป็นค่าใหม่จะเก็บค่าไว้ใน data <br>จากนั้นก็ไปอ่าน property effects ว่า foo property มีการผูกข้อมูลไว้ที่ไหนบ้าง <br>เมื่อ Polymer รู้ว่า foo ถูกผูกไว้ที่ &lt;div&gt; ก็จะทำการไปแก้ไขค่าของ div นั้นใน Template นั่นเอง เข้าใจปะ ไม่เข้าใจบอกได้นะ 555 .</p><pre><strong>set foo(val){</strong><br>   var old = this.__data.foo;<br>   if(old !== val){<br>      <strong>this.__data.foo = val;</strong><br>   }</pre><pre>   // Run property effects<br><strong>}</strong></pre><p><strong>แล้วถ้า</strong>เรามีการกำหนด Observer ให้ foo ละ Polymer จะทำอย่างไร</p><pre>static get properties() {<br>   return {<br>      foo: {<br>         type: String,<br>         value: &#39;Hello&#39;,<br>         <strong>observer: &#39;fooChanged&#39;</strong><br>      }<br>   };<br>}</pre><p>Polymer ก็จะทำประมาณว่าไปอ่านค่าใน static get properties ว่าแต่ละ Property มีการกำหนด Observer ไหม จากดูอย่างโค้ดด้านบน foo กำหนด Observer ให้ไปเรียก function ที่ชื่อว่า <strong>fooChange</strong></p><p>เมื่อเราเปลี่ยนแปลงค่า foo . Polymer ก็จะไปเรียก function <strong>fooChange </strong>ที่เรากำหนดนั่นเอง โดยมันจะเรียกประมาณนี้</p><pre>set foo(val){<strong><br>  </strong> // Dirty check new value<br>   // Run property effects<br>   <br>   <strong>this.fooChanged();</strong><br>}</pre><p><strong>แล้วถ้า</strong>เรามีการกำหนด Notify trueให้ foo ละ Polymer จะทำอย่างไร ( การกำหนด Notify คือการทำงานแบบ 2 Way Data binding)</p><pre>static get properties() {<br>   return {<br>      foo: {<br>         type: String,<br>         value: &#39;Hello&#39;,<br>         <strong>notify: true</strong><br>      }<br>   };<br>}</pre><p>Polymer ก็จะทำประมาณว่าไปอ่านค่าใน static get properties ว่าแต่ละ Property มีการกำหนด Notify true ไหม จากดูอย่างโค้ดด้านบน foo กำหนด Notify true ไว้</p><p>เมื่อเราเปลี่ยนแปลงค่า foo . Polymer ก็ทำการ dispatch event ขึ้นไปนั่นเอง ถ้า Element ที่สูงกว่าที่เรากำหนด และถ้า Element ที่สูงกว่ามีการกำหนด Annotaion {{ }} (2 Way Data binding) ไว้ ก็จะสามารถ set property ที่รอรับได้นั่นเอง</p><pre>set foo(val){<strong><br>  </strong> // Dirty check new value<br>   // Run property effects<br>   <br>   <strong>this.dispatchEvent(new CustomEvent(&#39;foo-changed&#39;))</strong><br>}</pre><p>ก็คอนเซ็ปการทำงานของ Polymer ก็ประมาณนี้ครับ</p><p>ทีนี้อย่างที่บอกครับว่า Polymer ทำการตรวจสอบข้อมูลเก่า แบบ Dirty checking ไม่มีผลกับการแก้สมาชิกภายใน Object หรือ Array หากเราแก้ข้อมูลชนิดดังกล่าว Polymer จะไม่ อ่าน Property effects ให้ครับเพราะว่ามันจะคิดว่าเป็นค่าเดิม ปัญหาแบบนี้มีทางออกครับ แต่เอาไว้ไปเจอกันใน EP หน้าครับ</p><p>สำหรับวันนี้ผมง่วงนอนแล้วฝันดีครับ… ^^</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e356cf4bf1c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dynamically creating element on polymer#2.0 นะจร๊ะ]]></title>
            <link>https://medium.com/@pongpanottassaneeyachumphalee/dynamically-creating-element-on-polymer-2-0-%E0%B8%99%E0%B8%B0%E0%B8%88%E0%B8%A3%E0%B9%8A%E0%B8%B0-4efceb36d78d?source=rss-a848b5f7c7ac------2</link>
            <guid isPermaLink="false">https://medium.com/p/4efceb36d78d</guid>
            <category><![CDATA[polymer]]></category>
            <category><![CDATA[dynamically]]></category>
            <category><![CDATA[web-components]]></category>
            <category><![CDATA[custom-elements]]></category>
            <dc:creator><![CDATA[Pongpanot Tassaneeyachumphalee]]></dc:creator>
            <pubDate>Fri, 09 Jun 2017 17:37:08 GMT</pubDate>
            <atom:updated>2017-06-09T18:06:37.801Z</atom:updated>
            <content:encoded><![CDATA[<p>ปกติเวลาเราจะแทรก Element สักหนึ่งตัวเนี้ย เราก็แค่ป้อน Tag ที่เราต้องการ กำหนด Attribute, Properties, Content เข้าไปใน Template แบบนี้</p><pre>&lt;template&gt;<br>   &lt;div id=&quot;container&quot;&gt;<br>      &lt;my-cat name=&quot;Miki&quot; emotion=&quot;{{emo}}&quot;&gt; meow &lt;/my-cat&gt;<br>   &lt;/div&gt;<br>&lt;/template&gt;</pre><p>แต่มีบาง“งาน”ครับ ที่ไม่สามารถที่ใช้วิธีด้านบนได้ เนื่องจากว่างานนั้นมีความถี่ในการสับเปลี่ยน Element ในจุดๆนั้นบ่อยมาก และไม่มี Element ที่ใช้ซ้ำกันเลย (Dynamic element) ดังนั้นเราต้องใช้วิธีการแทรก Element แบบ Dynamic ตามตัวอย่างด้านล่างครับ</p><pre>&lt;template&gt;<br>   &lt;div id=&quot;container&quot;&gt;&lt;/div&gt;<br>&lt;/template&gt;</pre><pre>static get properties() {<br>   return {<br>      emo: {<br>         type: String,<br>         value: &#39;Happy&#39;     <br>      }<br>  }<br>}</pre><pre>ready(){<br>   super.ready()</pre><pre>   this.myCat = document.createElement(&#39;my-cat&#39;)<br>   this.myCat.innerHTML = &#39; meow &#39;<br>   this.myCat.setAttribute(&#39;name&#39;,&#39;Miki&#39;)<br>   this.myCat.emotion = this.emo <br>   this.$.container.appendChild(this.myCat)</pre><pre>}</pre><p>ผลลัพธ์ก็จะออกแบบเดียวกันกับแบบแรกครับ เป็นไงละ… เอาไปใช้ได้หลายงานเลยนะคร๊าบบบ แต่เดี๋ยวก่อน!!! หากคุณเอาไปใช้.. ปัญหาที่ตามมาก็คือเรื่อง Data binding ครับ</p><p>เนื่องจากว่ามันไม่ได้แทรก Element แบบปกติมันก็เลยไม่สามารถทำ Data binding ได้ครับ จากตัวอย่างด้านบนเราก็แค่ เอาค่า this.emo จาก Element แม่ มาป้อนใส่ตรงๆให้กับ Element my-cat เท่านั้น ถ้าหากค่าของ Element แม่เปลี่ยน ​Element ลูกจะไม่เปลี่ยนตาม หากลูกเปลี่ยนแม่ก็ไม่เปลี่ยนเช่นกัน (2 way case)</p><p>ซึ่งมันก็มีวิธีแก้ไขปัญหาครับ แต่ก็ไม่ Perfect เท่าไหร่หรอกครับ แต่เรียกได้ว่าแก้ขัดได้ในระดับหนึ่ง มันเป็นการใช้ Observer และ Event เข้ามาช่วยในการ Binding ครับ</p><pre>//หากต้องการให้ข้อมูลไหลจาก Element แม่ไปลูก เราจะใช้ Observer ครับ</pre><pre>static get properties() {<br>   return {<br>      emo: {<br>         type: String,<br>         value: &#39;Happy&#39;,<br>         observer: &#39;_setVal&#39;<br>      }<br>  }<br>}</pre><pre>_setVal(val){</pre><pre>   if(this.myCat){<br>      if(this.myCat.emotion!=val)<br>      this.myCat.emotion = val<br>   }</pre><pre>}</pre><p>ข้อมูลไหลลงแล้ว ถึงตาไหลขึ้นบ้าง</p><pre>//หากต้องการข้อมูลจาก Element ลูกย้อนกลับไปหาแม่ เราจะใช้ Event ครับ</pre><pre>ready(){<br>   super.ready()</pre><pre>   this.myCat = document.createElement(&#39;my-cat&#39;)<br>   this.myCat.innerHTML = &#39; meow &#39;<br>   this.myCat.setAttribute(&#39;name&#39;,&#39;Miki&#39;)<br>   this.myCat.emotion = this.emo</pre><pre>   this.myCat.addEventListener(&#39;emotion-changed&#39;, this.getVal())</pre><pre>   this.$.container.appendChild(this.myCat)</pre><pre>}</pre><pre>getVal(_this){<br>   var _this = this<br>   return function (evt) {<br>      if(_this.emo != evt.detail.value)<br>         _this.emo = evt.detail.value<br>   };</pre><pre>}</pre><p>เพียงเท่านี้เราก็ได้ Data binding ฉบับแก้ขัดแล้วครับ</p><p>ถ้าหากคุณไม่ได้ต้องการให้ แม่กับลูก binding กัน แต่อยากให้ลูกกับลูกคุยกันแทนละ!! เราจะใช้ helper element ของ polymer ที่ชื่อว่า dom-bind</p><p>ซึ่ง dom-bind มันก็เป็น Element เอาไว้สำหรับให้ข้อมูล Binding กันได้ แม้จะไม่อยู่ใน polymer ก็ตาม พูดง่ายๆคือไม่ต้องมี Element แม่ ลูกก็คุยกันเองได้ ว้าวววว.. ไหมละ!!</p><p>แต่ในที่นี้เราจะมาใช้ช่วย Binding ข้อมูลระหว่างลูกกับลูก โดยมีแม่ครอบอยู่อีกที แต่ว่าข้อมูลจากแม่จะไม่สามารถไหลไปหาลูกนะครับ เพราะมันติด dom-bind นี่แหละ //จริงๆถ้าจะทำก็ทำได้ครับ</p><p>ว่าแล้วเราก็มาทำกันเลยดีกว่า . ทีนี้เรามาลองจินตนาการว่าเราจะสร้าง Element หน้าตาแบบนี้</p><pre>&lt;template&gt;<br>   &lt;div id=&quot;container&quot;&gt;</pre><pre>      &lt;dom-bind&gt;<br>         &lt;template&gt;<br>            &lt;my-cat name=&quot;Miki&quot; talk=&quot;{{say}}&quot;&gt; meow &lt;/my-cat&gt;<br>            &lt;my-dog name=&quot;Dig&quot; talk=&quot;{{say}}&quot;&gt; hong &lt;/my-dog&gt;<br>         &lt;/template&gt;<br>      &lt;/dom-bind&gt;</pre><pre>   &lt;/div&gt;<br>&lt;/template&gt;</pre><p>แต่เราจะเขียนแบบด้านล่างนี้นะ</p><pre>ready(){<br>   super.ready()</pre><pre>   var domBind = document.createElement(&#39;dom-bind&#39;)<br>   var template = document.createElement(&#39;template&#39;)<br>   domBind.appendChild(template)</pre><pre>   var doc = template.content.ownerDocument</pre><pre>   this.myCat = doc.createElement(&#39;my-cat&#39;)<br>   this.myCat.innerHTML = &#39; meow &#39;<br>   this.myCat.setAttribute(&#39;name&#39;,&#39;Miki&#39;)<br>   this.myCat.setAttribute(&#39;talk&#39;,&#39;{{say}}&#39;)</pre><pre>   this.myDog = doc.createElement(&#39;my-dog&#39;)<br>   this.myDog.innerHTML = &#39; hong &#39;<br>   this.myDog.setAttribute(&#39;name&#39;,&#39;Dig&#39;)<br>   this.myDog.setAttribute(&#39;talk&#39;,&#39;{{say}}&#39;)</pre><pre>   template.content.appendChild(this.myCat)<br>   template.content.appendChild(this.myDog)</pre><pre>   this.$.container.appendChild(domBind)</pre><pre>}</pre><p>เพียงเท่านี้พี่น้องก็คุยกันอย่าง Perfect แล้วครับ ไม่ต้องมีการดัก Observer ไม่ต้องมีการดัก Event</p><p>วิธีการ binding ข้อมูลแบบนี้มันจะคล้ายกับการทำงานภายใต้ผ้าคลุมของ Polymer เอ้า… งงละซิ Under the hood ไง ผ่าาม..</p><p>จริงๆแล้วแล้วเรื่อง Data Binding ของ ​Polymer ค่อนข้างที่จะปวดตับเลยทีเดียวครับ เนื่องจากมีเรื่อง Performance มาเกี่ยวข้อง Polymer จึงตั้งค่าเริ่มต้นที่ Dirty checking ครับ ใครที่เคยทำมาบ้างจะรู้ถึงจุดนี้ ซึ่งผมจะอธิบายอย่างละเอียดภายหลังครับ</p><p>ยังไงถ้า ผมผิดพลาดตรงไหนก็ ทักท้วงกันได้ครับ จะแก้ไขให้ อ่านไม่รู้เรื่องก็บอกหน่อยนะครับ ว่าจะให้ขยายความตรงไหนก็บอกกันครับ “ชั้น..รู้ดีว่าลวกๆเกินไป เย้ยยย…” //คือจริงๆก็พูดไม่รู้เรื่องอยู่แล้ว</p><p>สำหรับ บทความนี้ผมขอลาไปก่อน ง่วงนอนแล้ว ฝันดีครับ</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4efceb36d78d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>