{"id":6949,"date":"2016-03-25T11:14:32","date_gmt":"2016-03-25T15:14:32","guid":{"rendered":"http:\/\/www.ericfeminella.com\/blog\/?p=6949"},"modified":"2016-04-07T16:38:00","modified_gmt":"2016-04-07T20:38:00","slug":"polymer-behaviors-in-es6","status":"publish","type":"post","link":"https:\/\/www.ericfeminella.com\/blog\/2016\/03\/25\/polymer-behaviors-in-es6\/","title":{"rendered":"Polymer Behaviors in ES6"},"content":{"rendered":"<p>Being a typical aspect of Object Oriented Design, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Inheritance_(object-oriented_programming)\" target=\"_blank\">inheritance<\/a>, and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Mixin\" target=\"_blank\">mixins<\/a>, provide the means by which modular reuse patterns can be facilitated within a given system. Similarly, <a href=\"https:\/\/www.polymer-project.org\/1.0\/\" target=\"_blank\">Polymer<\/a> facilitates code reuse patterns by employing the notion of shared <a href=\"https:\/\/www.polymer-project.org\/1.0\/docs\/devguide\/behaviors.html\" target=\"_blank\">behaviors<\/a> modules. Let&#8217;s take a quick look at how to leverage them in Polymer when using <a href=\"https:\/\/babeljs.io\/docs\/learn-es2015\/#classes\" target=\"_blank\">ES6 classes<\/a>.<\/p>\n<h2>Implementing Behaviors<\/h2>\n<p>Implementing a <em>Behavior<\/em> is quite simple, just define an object within a block expression or an <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Glossary\/IIFE\" target=\"_blank\">IIFE<\/a>, and expose it via a <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Glossary\/Namespace\" target=\"_blank\">namespace<\/a>, or <a href=\"https:\/\/addyosmani.com\/writing-modular-js\/\" target=\"_blank\">module loader<\/a> of choice:<\/p>\n<pre class=\"lang:js decode:true\">\r\n'use strict';\r\n\r\n(() => {\r\n  \/\/ define the behavior\r\n  const SomeBehavior = {\r\n    ...\r\n  };\r\n  \/\/ add the behavior to a specific namespace ...\r\n  const ns = (window.SomeNamespace = window.SomeNamespace || {});\r\n  ns.SomeBehavior = SomeBehavior;\r\n})();\r\n<\/pre>\n<p><i>some-behaviors.js<\/i><\/p>\n<p>Then, include the behavior in a corresponding .html document of the same name so as to allow the behavior to be imported by subsequent elements:<\/p>\n<pre class=\"lang:html decode:true\">\r\n<script src=\"some-behaviors.js\" type=\"text\/javascript\"><\/script>\r\n<\/pre>\n<p><i>some-behavior.html<\/i><\/p>\n<h2>Extending Behaviors<\/h2>\n<p>After having defined and exposed a given Behavior, the Behavior can then be extended from element classes by defining a <i>behaviors<\/i> <code>getter \/ setter<\/code> as follows:<\/p>\n<pre class=\"lang:js decode:true\">\r\n'use strict';\r\n\r\n\/\/ define element class ...\r\nclass SomeElement {\r\n  beforeRegister(){\r\n    this.is = 'some-element';\r\n  }\r\n  \/\/ implement behaviors getter, returning an array of behaviors \r\n  \/\/ which are to be extended ...\r\n  get behaviors() {\r\n    return [window.SomeNamespace.SomeBehavior]\r\n  }\r\n  \/\/ implement corresponding setter ...\r\n  set behaviors(val) {\r\n    this._behaviors = val;\r\n  }\r\n}\r\n<\/pre>\n<p>Once the behavior has been extended, simply import the behavior in the element&#8217;s template (or element bundle, etc.) and it is available to the template class:<\/p>\n<pre class=\"lang:html decode=true\">\r\n<!-- import behavior module -->\r\n<link rel=\"import\" href=\"some-behavior.html\">\r\n<dom-module id=\"some-element\">\r\n    <template>\r\n      ...\r\n    <\/template>\r\n    <script src=\"some-element.js\" type=\"text\/javascript\"><\/script>\r\n<\/dom-module>\r\n<\/pre>\n<p><a href=\"http:\/\/codepen.io\/efeminella\/pen\/BjwbmQ\" target=\"_blank\">Try it<\/a><\/p>\n<h2>Implementing Multiple Behaviors<\/h2>\n<p>Similar to individual behaviors, multiple behaviors can also be defined and extended:<\/p>\n<pre class=\"lang:js decode:true\">\r\n'use strict';\r\n\r\n(() => {\r\n  \/\/ define first behavior ...\r\n  const FirstBehavior = {\r\n    properties: {\r\n      first: {\r\n        type: String,\r\n        value: 'First Behavior'\r\n      }\r\n    }\r\n  };\r\n  \/\/ add the behaviors to the given namespace ...\r\n  const ns = (window.SomeNamespace = window.SomeNamespace || {});\r\n  ns.FirstBehavior  = FirstBehavior;\r\n})();\r\n<\/pre>\n<p><em>first-behavior.js<\/em><\/p>\n<pre class=\"lang:js decode:true\">\r\n'use strict';\r\n\r\n(() => {\r\n  \/\/ define second behavior ...\r\n  const SecondBehavior = {\r\n    properties: {\r\n      second: {\r\n        type: String,\r\n        value: 'Second Behavior'\r\n      }\r\n    }\r\n  };\r\n  \/\/ add the behaviors to the given namespace ...\r\n  const ns = (window.SomeNamespace = window.SomeNamespace || {});\r\n  ns.SecondBehavior = SecondBehavior;\r\n})();\r\n<\/pre>\n<p><em>second-behavior.js<\/em><\/p>\n<p>In certain cases, I have found it helpful to group related behaviors together within a new behaviors (array) which bundles the individual behaviors together:<\/p>\n<pre class=\"lang:js decode:true\">\r\n'use strict';\r\n\r\n(() => {\r\n  \/\/ define first behavior ...\r\n  const FirstBehavior = {\r\n    properties: {\r\n      first: {\r\n        type: String,\r\n        value: 'First Behavior'\r\n      }\r\n    }\r\n  };\r\n  \/\/ define second behavior ...\r\n  const SecondBehavior = {\r\n    properties: {\r\n      second: {\r\n        type: String,\r\n        value: 'Second Behavior'\r\n      }\r\n    }\r\n  };\r\n  \/\/ define third behavior ...\r\n  const ThirdBehavior = {\r\n    properties: {\r\n      third: {\r\n        type: String,\r\n        value: 'Third Behavior'\r\n      }\r\n    }\r\n  }; \r\n  \/\/ define a behavior which bundles the related behaviors ...\r\n  const FourthBehavior = [\r\n      FirstBehavior, \r\n      SecondBehavior, \r\n      ThirdBehavior\r\n  ];\r\n\r\n  \/\/ add the behaviors to a specific namespace ...\r\n  const ns = (window.SomeNamespace = window.SomeNamespace || {\r\n    'FirstBehavior'  : FirstBehavior,\r\n    'SecondBehavior' : SecondBehavior,\r\n    'ThirdBehavior'  : ThirdBehavior,\r\n    'FourthBehavior' : FourthBehavior\r\n  });\r\n})();\r\n<\/pre>\n<p><em>Note: As can be seen in the FourthBehavior, a behavior can also be implemented as an Array of previously defined behaviors.<\/em><\/p>\n<h2>Extending Multiple Behaviors<\/h2>\n<p>As with extending individual behaviors, multiple behaviors can also be extended using a <i>behaviors<\/i> <code>getter \/ setter<\/code>. However, when extending multiple behaviors in ES6, there are syntactic differences which one must take note of. Specifically, the <i>behaviors<\/i> <code>getter<\/code> must be implemented as follows:<\/p>\n<pre class=\"lang:js decode:true\">\r\n\/\/ define element class ...\r\nclass CustomElement {\r\n  beforeRegister(){\r\n    this.is = 'custom-element';\r\n  }\r\n  \/\/ implement behaviors getter, returning an array of behaviors \r\n  \/\/ which are to be extended via this._behaviors, or, assigning \r\n  \/\/ and returning the array of behaviors to be extended should \r\n  \/\/ this._behaviors be undefined ...\r\n  get behaviors() {\r\n    const ns = window.SomeNamespace || {};\r\n    return this._behaviors || (this._behaviors = [\r\n      ns.FirstBehavior,\r\n      ns.SecondBehavior,\r\n      ns.ThirdBehavior\r\n    ]);\r\n  }\r\n  \/\/ implement corresponding setter ...\r\n  set behaviors(val) {\r\n    this._behaviors = val;\r\n  }\r\n}\r\n<\/pre>\n<p><a href=\"http:\/\/codepen.io\/efeminella\/pen\/MKOvOx\" target=\"_blank\">Try it<\/a><\/p>\n<p>And that&#8217;s basically all there is to it. Hopefully this article helped outline how Polymer Behaviors can easily be leveraged when implementing elements as ES6 classes. Enjoy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Being a typical aspect of Object Oriented Design, inheritance, and mixins, provide the means by which modular reuse patterns can be facilitated within a given system. Similarly, Polymer facilitates code reuse patterns by employing the notion of shared behaviors modules. Let&#8217;s take a quick look at how to leverage them in Polymer when using ES6 classes. Implementing Behaviors Implementing a&#8230; <a class=\"read-more\" href=\"https:\/\/www.ericfeminella.com\/blog\/2016\/03\/25\/polymer-behaviors-in-es6\/\">Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[23,246,78,45],"tags":[194,263,261,260,65,68,250,254,251,82,249],"class_list":["post-6949","post","type-post","status-publish","format-standard","hentry","category-design-patterns","category-es6","category-javascript-2","category-refactoring","tag-amd-2","tag-behaviors","tag-es6","tag-html-imports","tag-html5-2","tag-javascript","tag-polymer","tag-shadow-dom","tag-wct","tag-web","tag-web-components"],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/posts\/6949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/comments?post=6949"}],"version-history":[{"count":0,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/posts\/6949\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/media?parent=6949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/categories?post=6949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/tags?post=6949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}