arrow_backward Back to blog

Polymer Features You May Have Missed


This article originally appeared on the Divshot blog. Divshot is a developer-focused platform for deploying modern front-end applications. It was also started with Intridea's support by Michael Bleigh!

There's so much new ground to cover with Web Components and Polymer that oftentimes some of the smaller conveniences are overlooked. Here are some of the cool things Polymer can do (or does for you) that you might not have noticed.

Built-In Element Methods

You may have used fire() to trigger custom events from a Polymer element, but that's not the only convenient little method that's available.

async(fn, inArgs, inTimeout) allows you to delay the execution of code (similar to setTimeout or requestAnimationFrame) while keeping this bound appropriately. It comes in handy for things like allowing an event to fully propagate before performing a dependent action.

job(jobName, fn, inTimeout) gives you the ability to limit how frequently an action is performed. Jobs are only executed after inTimeout milliseconds have passed without the same job being called again. You might have heard of this pattern as debounce. It's great for handling things like listening for noisy event streams like keystrokes or mouse movements.

Layout Attributes

If you've browsed through Polymer examples and source, you might have come across attributes like layout, center, or flex sprinkled here and there. Polymer ships with declarative attributes that let you quickly and easily create complex layouts using CSS Flexbox.

Let's say I want to create a mobile-friendly layout with a header, footer, and content area.

There's quite a bit to this, and the official documentation does a great job of laying it out.

Auto-Binding Templates

Polymer's template binding is powerful stuff, but what if you want to use it outside the context of an element's Shadow DOM? Well, then it's auto-binding to the rescue!

The auto-binding template is an extension of the <template> tag that automatically binds the element itself as the model.

Because auto-binding templates can exist in the Light DOM (anywhere on the page, really) and don't depend on a user-defined custom element, they come in quite handy on occasion.

Attribute Reflection

Polymer makes it dead easy for attributes defined in the source code to configure properties on the DOM node, but did you know it goes both ways? By using a special form of published in your element definition, you can make it so that the attribute values change to match changing properties.

If you were to use the above element in your page and changed the name property in JavaScript, the name attribute in the web inspector would change, too. Cool stuff!

Polymer is a library that keeps surprising me with how deep and well-designed it is (without even mentioning all of the core and paper custom elements!). It's way more than just a set of polyfills.

Have any Polymer hidden gems of your own you'd like to share? Let us know!


New Project Request