Magento2: dynamic-rows / dynamic-rows-grid components are missing prototype / legacy dependency

Created on 1 Sep 2017  路  5Comments  路  Source: magento/magento2


Preconditions

  1. Magento 2.1.8
  2. server side irrelevant for issue

Steps to reproduce

  1. Implement a dynamic rows component in a page where prototype (legacy-build.min.js) is not defined as dependecy
  2. Try to use it
  3. When saving the listing grid, the dynamic rows component throws an error (max.each is not a function)

Expected result

  1. The item from the listing should show up in the dynamic rows grid

Actual result

  1. max.each is not a function
  2. There is no row being added to the dynamic rows grid

Code excerpt

At least this function in Magento_Ui/js/dynamic-rows/dynamic-rows-grid.js needs prototypejs:

        /**
         * Check changed records
         *
         * @param {Array} data - array with records data
         * @returns {Array} Changed records
         */
        _checkGridData: function (data) {
            var cacheLength = this.cacheGridData.length,
                curData = data.length,
                max = cacheLength > curData ? this.cacheGridData : data,
                changes = [],
                obj = {};

            max.each(function (record, index) {
                obj[this.map[this.identificationDRProperty]] = record[this.map[this.identificationDRProperty]];

                if (!_.where(this.cacheGridData, obj).length) {
                    changes.push(data[index]);
                }
            }, this);

            return changes;
        }

Workaround

Add a requirejs-config.js to your module which defines prototype as dependency:

var config = {
    shim: {
        'Magento_Ui/js/dynamic-rows/dynamic-rows-grid': {
            deps: ['prototype']
        }
        'Magento_Ui/js/dynamic-rows/dynamic-rows': {
            deps: ['prototype']
        }
    }
};

Solution

Add protoype as dependency to Magento_Ui/js/dynamic-rows/dynamic-rows.js:

define([
    'ko',
    'mageUtils',
    'underscore',
    'uiLayout',
    'uiCollection',
    'uiRegistry',
    'mage/translate',
    'prototype'
], function (ko, utils, _, layout, uiCollection, registry, $t) {
    'use strict';

    return uiCollection.extend({
        defaults: {
            defaultRecord: false,
            columnsHeader: true,
            columnsHeaderAfterRender: false,
            columnsHeaderClasses: '',
            labels: [],

Real solution

Remove prototype legacy stuff completely.
This took me frustratingly long to discover.
see also http://alanstorm.com/magento-2-prototypejs-still-lurks/

Clear Description Format is valid needs update bug report

Most helpful comment

Is this issue fixed? If not, why did you close it?

The steps are clear & easy to follow.
Even without reproducing this should be clear for a developer.

All 5 comments

@netzkollektiv , thank you for your report.
From your steps to reproduce we cannot completely understand how to reproduce issue. Please, give us your code source to reproduce this issue.

@netzkollektiv, we are closing this issue due to inactivity. If you'd like to update it, please reopen the issue.

Is this issue fixed? If not, why did you close it?

The steps are clear & easy to follow.
Even without reproducing this should be clear for a developer.

Was this ever fixed?

@tschirmer
They now use max.forEach instead of max.each, so I believe it is fixed.
But I did not have time yet to look back into it.

Was this page helpful?
0 / 5 - 0 ratings