Web Components interactions 25 Aug 2016
Hello everyone,
What this article is
I researched quickly how it is possible to interact with web components and I wanted to share my results. The idea behind this quick study was to understand how it would be possible to interact with a web components through various frameworks (angular, GWT, extjs), simply because this is the frameworks we use in my company. But before that I needed the basic understanding of web components.
What this article is not
This is not an introduction to web components. For that I suggest :
http://www.youtube.com/watch?v=XYlgxre_AF4
This article will also not discuss wether you should use or not use web components.
Just keep in mind web components are a group of specs
- Custom Elements
- HTML Imports
- Templates
- Shadow DOM
Interacting with web components
When I interact with a js library the type of interactions I want to do are usually
- Pass information to that library (options, data etc.)
- Being able to be recalled at some point to trigger some actions inside my business logic
If you take the example of Highcharts.js, you want to pass your data to draw a chart, then you migh want to be recalled when you click on a point to trigger some actions in your app.
So I tried to code a web component with those capabilities
Result
Here is the result https://github.com/ronanquillevere/chromeonly-element
How it looks (img)
The index.html
page
My component creation code
The html template used
Some explanations
So what I did, appart from creating a very basic web component, is simply enrich the prototype of my web component so that I can pass data to it and callbacks
This is how I pass data to my web component, first I retrieve it then I call the render method defined on the element prototype
var wc = document.querySelector('#wc1');
wc.render(
{
name : 'hello web component 1',
onButton1Clicked : function()
{
alert('wc1 button 1 clicked');
}
});
On the element prototype I have the following method. Note that this
is the object calling the method so this.wcOptions is set on the web component object and not on the prototype.
element.render = function(wcOptions)
{
this.wcOptions = wcOptions;
this.titleZone.textContent = this.wcOptions.name;
console.log('rendered');
};
For the click button callbacks the only trick is to manage properly how to pss the this
object. The createdCallback method is called by the web component object, so this
is the instance calling the method. Because in Javascript method are bound when executed and not when they are declared I need to save and pass the right reference to the this object to the button click listeners.
element.registerButtonsCallbacks = function(rootElement)
{
var button1 = rootElement.querySelector('#button1');
var button2 = rootElement.querySelector('#button2');
var self = this;
button1.addEventListener('click', function(){
element.clickButton1(self);
});
button2.addEventListener('click', function(){
element.clickButton2(self);
});
};
Then my clickButton1 prototype method will check if a callback has been defined in the options passed to the render method.
element.clickButton1 = function(that)
{
if (that.wcOptions.onButton1Clicked)
that.wcOptions.onButton1Clicked();
else
console.log('button1 was clicked but no callback is set');
};
Hope this article will help some of you.