Search code examples
polymerpolymer-2.xpolymer-3.x

Polymer 3 Mixin - Possible to implement host property in mixin like in Polymer 1 behaviors?


I am converting a Polymer 1 behavior to a Polymer 3 mixin.

With the Polymer 1 behaviors, I was able to place a host property in the behavior. Is that possible with Polymer 3 Mixins?

Polymer 1 behavior:

<script>
  AccountBehavior = {
    properties: {
      tabactivated: Boolean
    },

    observers: ['_refreshActivePosts(tabactivated)'],

    _refreshActivePosts: function(tabactivated) {
      if (tabactivated) {
        this.$.account.refreshAjax();
      }
    }
  }
</script>

Solution

  • Not sure I can remember exactly what the old host property does. But I have this module which I wrote to find the host of an element

    export default function domHost(self) {
      let parent = self.parentNode;
      while(parent && parent.nodeType !== 11) {
        parent = parent.parentNode;  //work up the hierarchy
      }
    
      return parent ? parent.host : self;
    }
    

    I use it quite a lot to add event listeners to my hosting element something like this:-

     connectedCallback() {
        super.connectedCallback();
        this.domHost = domHost(this);
        this.domHost.addEventListener('pas-filelocation-request', this._gotRequest);
      }
      disconnectedCallback() {
        super.disconnectedCallback();
        this.domHost.removeEventListener('pas-filelocation-request', this._gotRequest);
      }