1

I have created a custom module that uses the UI component

<uiComponent name="product_listing"/>

Basically, I want to create a javascript file in my module that rewrites/overrides only the fallowing part of that component js file:

vendor/magento/module-ui/view/base/web/js/grid/provider.js:

/**
 * Reloads data with current parameters.
 */
    reload: function () {
        this.trigger('reload');

        if (this.request && this.request.readyState !== 4) {
            this.request.abort();
        }
        console.log(this);
        this.request = $.ajax({
            url: this['update_url'],
            method: 'GET',
            data: this.get('params'),
            dataType: 'json'
        });

        this.request
            .done(this.onReload)
            .error(this.onError);
    },

Can someone please guide me trough how to do that in the best and efficient way ? Thank you!

0

1 Answer 1

1

You can try 'mixins' feature, all you need is to create requireJS module with extended method like this:

default_mixin.js:

define(function () {
    'use strict';

    return function (target) { // target == Result that Magento_Ui/... returns.
        var parent_reload = target.reload;
        target.reload = function () {
            console.log('before reload');
            parent_reload.apply(this, arguments); //call parent reload
            console.log('after reload');
        }
        return target; // new result that all other modules receive 
    };
});

requirejs-config.js:

config: {
    mixins: {
        'Magento_Ui/web/js/grid/provider.js': {  // Target module
            'My_Module/js/payment/default_mixin': true  // Extender module
        }
    }
}
5
  • Not working.... var config = { config : { mixins: { 'Magento_Ui/web/js/grid/provider': { // Target module 'MyVendor_MyModule/js/default_mixin': true // Extender module } } } }; .. and the code in default_mixin.js is exact copy/paste from the example code you gave, but no console.log or alert what so ever... Commented Feb 29, 2016 at 12:12
  • is this still a valid solution, because i see its over 6 months old ? github.com/magento/magento2/issues/1864 Commented Feb 29, 2016 at 12:13
  • tried this as code for default_mixin ... not working as well ... define(function () { 'use strict'; var mixin = { reload:function (){ console.log('ddd'); alert('3333'); } } return function (target) { // target == Result that Magento_Ui/.../default returns. return target.extend(mixin); // new result that all other modules receive }; }); Commented Feb 29, 2016 at 12:21
  • I remove pub/static and deploy new static content after every change... what exactly is arguments ? Commented Feb 29, 2016 at 12:22
  • yeah, with a mixture between your answer and the github one I made it work, thank you! Commented Feb 29, 2016 at 14:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.