mixin vs plugin. What to choose?
Let's assume I have defined several new com开发者_如何学Cponents -MyComponent1
, MyComponent2
, ..., which extend Ext.Component
.
Now I wnat to extend all this widgets with the same functionality - I want to add close
button which would appear at the top-right position of MyComponentX.el
.
What should I use: mixin
? plugin
? or something else?
mixin vs plugin
, what is the best practise?I like to think about it as -
mixin
is what you use to achieve 'multiple inheritence'
plugin
is what you use to enhance an existing component. plugin's lifecycle is managed by the component. (no lifecycle managemen for mixin. think of mixin as base class equivalent)
Mixin properties and functions are injected into the class directly so they are part of the class definition.
plugins are added at the instance level
Plugins will add functionality to the Ext.Component class or the class extending Ext.Component. Scope of any method in plugin is in plugin itself.
Mixins can be used to add functionality to any other class. mixins methods will be available directly into the class.
精彩评论