Skip to content

Commit

Permalink
Fix minicart promotion region not rendering
Browse files Browse the repository at this point in the history
The minicart promotion region was not rendering due to
a wrongly written check for the number of items in the
'promotion' region. The code checked for the length
property of the observable returned from the getRegion
method instead of the array it contained.

A fast way would have been to add the missing parentheses
to the check (and I made antoher PR doing just that), but
I felt that the getRegion(region)().length pattern is a
bit hard to read and easy to make a mistake with.

Therefore, this commit adds a new method to the
lib/core/collection.js component of the Magento_Ui module
that can be used to check if a region has any elements
associated with it. This commit also replaces all occurences
of getRegion(region)().length with a call to the new method.
  • Loading branch information
mattijv committed Oct 30, 2019
1 parent 65763df commit 20d3f40
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
<render args="$col.getBody()"/>
</fastForEach>

<div if="getRegion('action-primary-area')().length || getRegion('action-secondary-area')().length"
<div if="regionHasElements('action-primary-area') || regionHasElements('action-secondary-area')"
class="product-item-actions">
<div class="actions-primary" if="getRegion('action-primary-area')().length">
<div class="actions-primary" if="regionHasElements('action-primary-area')">
<fastForEach args="data: getRegion('action-primary-area'), as: '$col'" >
<render args="$col.getBody()"/>
</fastForEach>
</div>

<div if="getRegion('action-secondary-area')().length"
<div if="regionHasElements('action-secondary-area')"
class="actions-secondary"
data-role="add-to-links">
<fastForEach args="data: getRegion('action-secondary-area'), as: '$col'" >
Expand All @@ -42,7 +42,7 @@
</div>
</div>

<div if="getRegion('description-area')().length"
<div if=regionHasElements('description-area')"
class="product-item-description">
<fastForEach args="data: getRegion('description-area'), as: '$col'" >
<render args="$col.getBody()"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ define([
*/
isPaymentMethodsAvailable: function () {
return _.some(this.paymentGroupsList(), function (group) {
return this.getRegion(group.displayArea)().length;
return this.regionHasElements(group.displayArea);
}, this);
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
</div>
</div>

<div id="minicart-widgets" class="minicart-widgets" if="getRegion('promotion').length">
<div id="minicart-widgets" class="minicart-widgets" if="regionHasElements('promotion')">
<each args="getRegion('promotion')" render=""/>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class="items payment-methods">
<div repeat="foreach: paymentGroupsList, item: '$group'"
class="payment-group">
<div if="getRegion($group().displayArea)().length"
<div if="regionHasElements($group().displayArea)"
translate="getGroupTitle($group)"
class="step-title"
data-role="title">
Expand Down
12 changes: 12 additions & 0 deletions app/code/Magento/Ui/view/base/web/js/lib/core/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@ define([
return regions[name];
},

/**
* Checks if the specified region has any elements
* associated with it.
*
* @param {String} name
* @returns {Boolean}
*/
regionHasElements: function (name) {
var region = this.getRegion(name);
return region().length > 0;
},

/**
* Replaces specified regions' data with a provided one.
* Creates region if it was not created yet.
Expand Down

0 comments on commit 20d3f40

Please sign in to comment.