Getting declarative with Polymer

Polymer is Google’s implementation of WebComponents for today’s browser. If you are not familiar with it, check out this nice tutorial by Pankaj Parashar. The main idea behind WebComponents is that we should be able to create our own web components (hence the name). A web component is just a custom HTML element with some styles and behavior attached to it. An example can be the following:

Polymer provides two sets of components one can use directly or build on:

  • Core – basic elements, both visual and non-visual, that can help one built layouts, scaffolding, and user interaction
  • Paper – these elements implement the material design philosophy of Google

Besides some standard elements in the core set (f.x., core-dropdown, core-menu, core-tooltip), one can find non-visual elements that perform some action. Such an element is core-ajax. What it does is basically an AJAX request. Here is how you can make use of it on your page:

I wrote a simple HTML site that fetches a list of videos related to a search string. It demonstrates one way of using Polymer’s core elements.

And you need a couple of good old JavaScript lines underneath:

Another similar HTML element is core-signals, which provides basic publish-subscribe functionality. This reminds me a lot to ASP.NET, where one can use non-visual ASP.NET server controls and instruct them to ahieve a goal, f.x., SqlDataSource. The nice thing about these non-visual elements is they are very good for prototyping. In some small projects where you want to check whether a concept works, it can be quite convenient to use them. However, in my personal opinion, these behavior-only elements are not suitable for large projects, because we couple too much the visual representation (i.e. the views) with the “business” logic (that should be part of some controller or service). In this case, I would rather stick to traditional ways of firing an AJAX request. 🙂