Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymer multiple inheritance/composition

Polymer website says multiple inheritance (or composition) is not supported using 'extend' attribute in Polymer. I want an element to be composed of some methods from one Polymer element, and some others from another, to have it reflect application logic. Is there currently any way to implement that in Polymer? (like doing that using javascript mixins)

like image 347
sepans Avatar asked May 14 '14 16:05

sepans


2 Answers

Polymer now supports mixin:

var mixinObj = {
   foo: function() {
     /* ... */
   }
};

var mixinObj2 = {
   foo2: function() {
     /* ... */
   }
};


Polymer('my-component', Polymer.mixin({   // Platform.mixin for polymer version < 0.5
  bar: function() {

     /* ... */
     this.foo();   // all the functions in mixinObjs are now accessible through 'this'   
     this.foo2();   



  }


}, mixinObj, mixObj2);  // Platform.mixin accepts multiple mixin objects

More info here

like image 93
sepans Avatar answered Oct 06 '22 00:10

sepans


I can't speak to the reasoning of the Polymer folks, but it's generally considered preferable to use composition over inheritance.

like image 36
Peter Burns Avatar answered Oct 06 '22 01:10

Peter Burns