/
Mixin Mechanism (4h)

Mixin Mechanism (4h)

A module can add a method to a class from core or other module.

Config Mixin

In the module, we register a mixin in file etc/config.js.

For example: We add mixin for MenuComponent

import ModuleConfigAbstract from "../../ModuleConfigAbstract"; class HelloWorldConfig extends ModuleConfigAbstract{ module = ['helloworld']; mixin = { component: { MenuComponent: { // new method name: mixin method implementation setOrder: function(order) { this.order = order; return this; }, getOrder: function() { return this.order; }, // Static methods static: { plus: function(a, b) { return b + a; } } } } }; } export default (new HelloWorldConfig());

We have 7 type of class can be plugged in:

  1. service

  2. resource_model

  3. repository

  4. container

  5. component

  6. data_resource

To determine which type of class, we can find Factory type in source code. Example:

/** * * @type {MenuComponent} */ const component = ComponentFactory.get(MenuComponent);

Exercise:

  • Create 1 Mixin contain the function to alert the Grand total of order to order detail component

  • Use plugin, add button “Show Grand Total” to order detail component

  • When click that button, call the function in Mixin, show grand total of order

 

Related content

Plugin Mechanism (4h)
Plugin Mechanism (4h)
More like this
Rewrite Class (4h)
Rewrite Class (4h)
More like this
Layout Mechanism (4h)
Layout Mechanism (4h)
More like this
Plugin/Rewrite (5h)
Plugin/Rewrite (5h)
More like this
Event Observer (4h)
Event Observer (4h)
More like this
Magestore POS Customization
Magestore POS Customization
Read with this