Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Magento 2 mixins (extend/override for object literals)
/*
* Magento 2 mixins for js files that return an object literal
* (based on https://alanstorm.com/the-curious-case-of-magento-2-mixins/)
* (for files that return an UIComponent: https://gist.github.com/alecarg/ed0516132e9c8a31c66f13fbccd292cf)
* (for files that return a function: )
*/
define([
'mage/utils/wrapper'
], function (wrapper) {
'use strict';
return function(targetModule){
/*
* Extend
*/
targetModule.someMethod = wrapper.wrap(targetModule.someMethod, function(originalFn){
// do something before
var result = originalFn(); // call original method (if needed)
// do something after
return result; // return original value
});
/*
* Override
*/
targetModule.someOtherMethod = wrapper.wrap(targetModule.someFn, function(){
/* copy code from someOtherFn here if needed or write your own; also attempt to
return in line with what the original fn returned to avoid breaking anything */
});
return targetModule;
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.