{"id":34029,"date":"2016-05-03T11:01:36","date_gmt":"2016-05-03T05:31:36","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=34029"},"modified":"2016-07-30T06:21:50","modified_gmt":"2016-07-30T00:51:50","slug":"introduction-to-polymerjs","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/introduction-to-polymerjs\/","title":{"rendered":"Introduction to PolymerJS: Building Web Components"},"content":{"rendered":"<p>In the history of web development there have been many changes. Day by day new innovations were introduced in the market to make web more interative and meet user expectations. Javascript has evolve a lot due to these innovations but HTML hasn&#8217;t grown that much. So at the end of the day we end up in writting more and more javascript code to meet user expectations.<\/p>\n<p>In the hussel of the new upcoming technologies <strong>Web Components<\/strong> were introduced in web standards to create our very own <strong>custom HTML elements<\/strong> which behaves just like any other basic HTML element sitting there on web. Web components covered very important aspect of programing and that is <strong>reusability<\/strong> ( i.e develop once and use anytime) and <strong>isolated scope<\/strong> that allows libraries to work without conflicting each other.<\/p>\n<p>Web Component was the coolest solution to these problem but the only limitation is that they are not supported across all the browsers. Google being enthusiastic to use this bleeding edge technology started <strong>Polymer project<\/strong> and developed a polyfill <strong>webComponent-lite.js<\/strong> that provides support for webcomponents.<\/p>\n<p><strong>Polymer project<\/strong> was introduced with one thing in mind &#8211; making reusable components that just work out of the box without too much hassle. Polymer team has <a title=\"Polymerjs Development\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">developed <strong>PolymerJS<\/strong><\/a> which use all core technologies of Web Component and allows us to create our custom component in very easy and cleaner way.<\/p>\n<p>In this blog we will learn how to integrate Polymer in our application in very easy steps, making use of Polymer\u2019s reusable components.<\/p>\n<p><strong>Adding Polymer as a dependency:<\/strong><\/p>\n<p>The easiest way to add Polymer to your project is through bower. If you have been using bower, then you can simply do :<\/p>\n<p>[code language=&#8221;bash&#8221;]<br \/>\nmkdir my-polymer-project &amp;&amp; cd $_<br \/>\nbower install Polymer\/polymer<br \/>\n[\/code]<\/p>\n<p>This will create a bower_components directory in the project folder with the Polymer dependency. Don\u2019t worry about all the files that have been added. We\u2019ll take a look at them at a later stage.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-34024 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/04\/Screen-Shot-2016-04-29-at-12.49.39-AM.png\" alt=\"Screen Shot 2016-04-29 at 12.49.39 AM\" width=\"276\" height=\"354\" \/><\/p>\n<p>If you\u2019re not comfortable with bower, you can directly download the zip file from <a href=\"https:\/\/www.polymer-project.org\/\">https:\/\/www.polymer-project.org\/<\/a> and extract the zip file in your project folder.<\/p>\n<p><span style=\"text-decoration: underline;\"><em><strong>Web Components Polyfill<\/strong><\/em><\/span><\/p>\n<p>An important library that\u2019s added as part of the Polymer package is a polyfill library named \u201cwebcomponents.js\u201d in the above image. Polymer builds its entire framework on standards documented as part of the \u201cWeb Components\u201d standards.\u00a0However, since these standards are not supported across all browsers, Polymer uses this\u00a0polyfill library to somewhat bridge the implementation gaps between browsers.<\/p>\n<p><em><span style=\"text-decoration: underline;\"><strong>Using pre-built components<\/strong><\/span><\/em><\/p>\n<p>Let\u2019s get started now! In order to get started quickly, the Polymer team has provided a vast range of reusable elements that we can use in creating our own webpage and apps. These elements are themselves built with the Polymer library and are available for use directly. The catalog of pre-built components is listed <a title=\"here\" href=\"https:\/\/elements.polymer-project.org\/browse\">here<\/a>.<\/p>\n<p>To use any element from the collections mentioned, we can either install one element at a time or we can install the whole collection as well. For example if we have to use google-map component from Google Web Components suite, then install it using bower :<\/p>\n<p>[code language=&#8221;bash&#8221;]<br \/>\nbower install GoogleWebComponents\/google-web-components<br \/>\n[\/code]<\/p>\n<p>OR<\/p>\n<p>[code language=&#8221;bash&#8221;]<br \/>\nbower install GoogleWebComponents\/google-map<br \/>\n[\/code]<\/p>\n<p>Once all the dependencies are in place, we can simply use the element by importing the requisite HTML file of the component. Let\u2019s quickly use the google-map element to render a Google map (surprise!).<\/p>\n<p>[code language=&#8221;html&#8221;]<br \/>\n&lt;html lang=&quot;en&quot;&gt;<br \/>\n&lt;head&gt;<br \/>\n    &lt;meta charset=&quot;UTF-8&quot;&gt;<br \/>\n    &lt;title&gt;My Google Map&lt;\/title&gt;<br \/>\n    &lt;script src=&quot;bower_components\/webcomponentsjs\/webcomponents.min.js&quot;&gt;&lt;\/script&gt;<br \/>\n    &lt;link rel=&quot;import&quot; href=&quot;bower_components\/google-map\/google-map.html&quot;&gt;<\/p>\n<p>&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;style&gt;<br \/>\n  google-map {<br \/>\n    height: 800px;<br \/>\n  }<br \/>\n&lt;\/style&gt;<br \/>\n&lt;google-map&gt;&lt;\/google-map&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<br \/>\n[\/code]<\/p>\n<p>And there you have it! Few lines of code and you have a full fledged working Google map on your page. You can also specify more options for the custom element. We can also add our own style classes on different properties of elements as we have done above in the code snippet.<\/p>\n<p>This is a very basic example of how web components can make web development easier by taking out the pain involved in integration and conflicting dependencies. In the next blog, we\u2019ll take a look at how we can build our own custom elements to build up a very basic webpage.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the history of web development there have been many changes. Day by day new innovations were introduced in the market to make web more interative and meet user expectations. Javascript has evolve a lot due to these innovations but HTML hasn&#8217;t grown that much. So at the end of the day we end up [&hellip;]<\/p>\n","protected":false},"author":72,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1],"tags":[3259,398,3257,1173,3258],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/34029"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/72"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=34029"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/34029\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=34029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=34029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=34029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}