SimpleMagento - Magento tutorials, tips and discussions with simplicity in mind
  • Magento 2
  • Frontend
  • Dev Talk
  • Checkout
  • UI components
  • Online Marketing
  • eCommerce Talk
  • Magento 2
  • Frontend
  • Dev Talk
  • Checkout
  • UI components
  • Online Marketing
  • eCommerce Talk
SimpleMagento - Magento tutorials, tips and discussions with simplicity in mind
No Result
View All Result
Home Magento 2

Magento 2 UI components and listener

by freelancer
March 27, 2022
0 0
Share on FacebookShare on Twitter

In this short and simple example we will explain how to create listener in Magento 2 UI component. We will explain it on built in Magento 2 UI component Columns which use component Magento_Ui/js/grid/listing (part of admin grid interface).

This file listing.js represents knockoutjs viewmodel of our component “Columns“. Let’s start with an explanation. Take a look at our listing.js code:

define([
    'ko',
    'underscore',
    'Magento_Ui/js/lib/spinner',
    'rjsResolver',
    'uiLayout',
    'uiCollection'
], function (ko, _, loader, resolver, layout, Collection) {
    'use strict';
 
    return Collection.extend({
        defaults: {
            template: 'ui/grid/listing',
            stickyTmpl: 'ui/grid/sticky/listing',
            viewSwitcherTmpl: 'ui/grid/view-switcher',
            positions: false,
            displayMode: 'grid',
            displayModes: {
                grid: {
                    value: 'grid',
                    label: 'Grid',
                    template: '${ $.template }'
                }
            },
            dndConfig: {
                name: '${ $.name }_dnd',
                component: 'Magento_Ui/js/grid/dnd',
                columnsProvider: '${ $.name }',
                enabled: true
            },
            editorConfig: {
                name: '${ $.name }_editor',
                component: 'Magento_Ui/js/grid/editing/editor',
                columnsProvider: '${ $.name }',
                dataProvider: '${ $.provider }',
                enabled: false
            },
            resizeConfig: {
                name: '${ $.name }_resize',
                columnsProvider: '${ $.name }',
                component: 'Magento_Ui/js/grid/resize',
                enabled: false
            },
            imports: {
                rows: '${ $.provider }:data.items'
            },
            listens: {
                elems: 'updatePositions updateVisible',
                '${ $.provider }:reload': 'onBeforeReload',
                '${ $.provider }:reloaded': 'onDataReloaded'
            },
            modules: {
                dnd: '${ $.dndConfig.name }',
                resize: '${ $.resizeConfig.name }'
            },
            tracks: {
                displayMode: true
            },
            statefull: {
                displayMode: true
            }
        }
...

In our listing.js class you will notice configuration for listener in “defaults” objects as property “listens“:

listens: {
                elems: 'updatePositions updateVisible',
                '${ $.provider }:reload': 'onBeforeReload',
                '${ $.provider }:reloaded': 'onDataReloaded'
            }

What doest it mean?

When provider (data source), in our case class “Magento_Ui/js/grid/provider“, fires event “reload” with method “this.trigger(‘reload’)” then callback method “onBeforeReload” will be called in our component “Magento_Ui/js/grid/listing“.

Also, when request in providers method reload is resolved (done), then “onReload” method is called and in “onReload” method event “reloaded” is fired. That triggers callback method “onDataReloaded” in viewmodel of component “Columns”.

If you take a look on provider js file you will see exactly where provider class fires events:

reload: function (options) {
            var request = this.storage().getData(this.params, options);
 
            //call to callback method onBeforeReload
            this.trigger('reload');
 
            request
                .done(this.onReload)
                .fail(this.onError);
 
            return request;
        },
onReload: function (data) {
            this.firstLoad = false;
 
            //call to calback method onDataReloaded
            this.setData(data)
                .trigger('reloaded');
        }

In method “onReload” event “reloaded” is fired by method “trigger(‘reloaded’)“.

Reload method in provider class is triggered always when UI component loads data from Magento 2 backend.

I hope that you understand our explanation and that you understand how it works.

Tags: UI componentsMagento 2
Previous Post

File upload in Magento 2 store configuration

Next Post

We’ve developed a Croatian Language Pack for Magento 2

freelancer

freelancer

Related Posts

Magento 2

Implementing payment gateway in Magento 2 – Sample 1

April 22, 2022
Magento 2

Implementing payment gateway in Magento 2 Spinned – Sample 1

April 22, 2022
Magento 2 Payment

Implementing payment gateway in Magento 2 – Sample 1

April 22, 2022
Magento 2

Routing in Magento 2 – Sample 1

April 22, 2022
MVC Magento 2

Magento 2 Controllers – Sample 1

April 22, 2022
Magento 2

How to create a basic module in Magento 2 – Sample 1

April 22, 2022

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Administration
  • Search
  • Configuration
  • Starting Up
  • Extensions
  • News
  • PWA
  • Magento 2 API
  • Programming
  • MVC Magento 2
  • UX/UI Design
  • Shipping Magento 2
  • Database
  • Magento 2 Payment
  • Magento 2
  • Cache
  • Frontend
  • Integration
  • Dev Talk
  • Life at Inchoo
  • Checkout
  • Tips
  • UI components
  • Products
  • Online Marketing
  • Debugging
  • Magento
  • Search Magento 2
  • Upgrading Magento 2
  • Marketing
  • eCommerce Talk
  • Events & Observers
  • Uncategorized

Popular Post

Magento 2

Magento 2 frontend architecture – Sample 1

April 22, 2022
Magento 2

Magento 2 Luma Theme Under The Scope

March 27, 2022
Magento 2 Payment

Implementing payment gateway in Magento 2 – Sample 1

April 22, 2022
Dev Talk

Moving the validation error message, this time globally

March 25, 2022
No Result
View All Result
[vc_row full_width="stretch_row" vc_row_background="" css=".vc_custom_1516082354216{margin-top: 30px !important;padding-top: 22px !important;padding-bottom: 22px !important;}"][vc_column el_class=".footer_center" offset="vc_col-lg-offset-3 vc_col-lg-6"]
[vc_empty_space height="15px"][vc_column_text css=".vc_custom_1516083863519{margin-bottom: 0px !important;}" el_class=".copyright"]Copyright © 2018 JNews. Photography Blog theme by Jegtheme.[/vc_column_text][/vc_column][/vc_row]
No Result
View All Result
  • Magento 2
  • Frontend
  • Dev Talk
  • Checkout
  • UI components
  • Online Marketing
  • eCommerce Talk

© 2023 JNews - Premium WordPress news & magazine theme by Jegtheme.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In