5

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)

Cœur
  • 37,241
  • 25
  • 195
  • 267
sepans
  • 1,372
  • 13
  • 24
  • I would also like to know if not allowing multiple inheritance is because of implementation complexity or a design decision? – sepans May 14 '14 at 16:01

3 Answers3

7

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

sepans
  • 1,372
  • 13
  • 24
2

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

Community
  • 1
  • 1
Peter Burns
  • 44,401
  • 7
  • 38
  • 56
  • Thanks, you are right. But I actually meant the same thing, which is, what are the composition mechanisms in Polymer, but my use of strong 'class based inheritance' terminology was misleading. I will slightly edit the question to make it more clear. – sepans May 16 '14 at 15:14
  • I'd like to see the answer to that question too. I'd recommend posting another question. – Peter Burns May 17 '14 at 00:29
0

Polymer support Mixin concept for overcome the Multiple inheritance concept.

Example :

Class ElementOne extends Polymer.Element {
   ready() {
     super.ready();   
   }
}

Class ElementTwo extends Polymer.Element {
  ready() {
     super.ready();
  }
}

Class ElementThree extends ElementOne(ElementTwo(Polymer.Element)) {
  ready() {
     super.ready();
  }
}

I hope it's helpful to you.

Kumar Ramalingam
  • 349
  • 3
  • 15