Polymer Multiple Mixin
I’m building applications with just (Polymer) web components. Yes, they’re that powerful.
About the time I read Inheritance and composition with Polymer,
I noticed several of my custom elements could handle a little refactoring. Pascal Precht recommends Platform.mixin
in this case. While it has limited documentation, you can find the signature here.
The straight-forward approach left a bad taste in my mouth. Firstly, my mixed-in objects lazed around in the global scope like they owned the place. Secondly, I didn’t like the code structure of multiple mixins. Lastly, I wanted the new implementation to have the ability to override capabilities (like a prototype).
In the above example, sharedA
’s properties get copied over to the result of sharedB
’s mixin over my-element
.
On a large my-element
object, you wouldn’t notice what capabilities you inherited until the end of the page.
This example accomplishes the same thing and solves the problem of secret “parents.” In this case, sharedB
gets overridden by sharedA
. To see this, change the second line to var sharedB = {a:2};
. Priority goes
from left-to-right.
This pattern shows us our mixin chain upfront and allows our new object get “last say” on what capabilities the
final object will hold. There’s just one, big, problem–we permanently mangled sharedB
. Even worse, we may
have messed up my-element
for future instances because of how Polymer caches non-primitive properties.
So, I wrote a little utility to keep the parts I liked and stop the mangling of my shared code objects.
First, make a component folder called app with an app.html file in the folder.
Place the following in components/app/scripts/app.js
Now, include this feature in your other components using this:
In my component code, I use it like this:
Comments welcome.