Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit b6fa1a6

Browse files
serhiyzhovnirjeff-matthews
authored andcommitted
Added example of mixin for jQuery Widget (#5229)
* Add example of a mixin for jQuery Widget and adjusted the mixin documentation * Fixed the link * Adjusted the JS mixins documentation * Fixed the issue with trailing spaces
1 parent 2afa6b4 commit b6fa1a6

File tree

1 file changed

+60
-18
lines changed

1 file changed

+60
-18
lines changed

guides/v2.2/javascript-dev-guide/javascript/js_mixins.md

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,71 @@ This function accepts a target component(module) as an argument and returns a mo
3838

3939
This allows you to return a new instance of the target component with your modifications attached to it before it is used in the application.
4040

41-
### Example
41+
### Examples
42+
43+
#### Extend UI Component
4244

43-
The following is an example of a mixin module that extends the `target` component with a function that introduces a new `blockVisibility` property to a column element.
45+
The following is an example of a mixin that extends the `target` component with a function that introduces a new `blockVisibility` property to a column element.
4446

4547
**File:** `OrangeCompany/Sample/view/base/web/js/columns-mixin.js`
4648

4749
```javascript
48-
4950
define(function () {
50-
'use strict';
51+
'use strict';
52+
53+
var mixin = {
54+
55+
/**
56+
*
57+
* @param {Column} elem
58+
*/
59+
isDisabled: function (elem) {
60+
return elem.blockVisibility || this._super();
61+
}
62+
};
63+
64+
return function (target) { // target == Result that Magento_Ui/.../columns returns.
65+
return target.extend(mixin); // new result that all other modules receive
66+
};
67+
});
68+
```
5169

52-
var mixin = {
70+
#### Extend jQuery Widget
5371

54-
/**
55-
*
56-
* @param {Column} elem
57-
*/
58-
isDisabled: function (elem) {
59-
return elem.blockVisibility || this._super();
60-
}
61-
};
72+
The following is an example of a mixin that extends the [modal widget][] with a function that adds confirmation for a modal closing.
6273

63-
return function (target) { // target == Result that Magento_Ui/.../columns returns.
64-
return target.extend(mixin); // new result that all other modules receive
65-
};
66-
});
74+
**File:** `OrangeCompany/Sample/view/base/web/js/modal-widget-mixin.js`
6775

76+
```javascript
77+
define(['jquery'], function ($) {
78+
'use strict';
79+
80+
var modalWidgetMixin = {
81+
options: {
82+
confirmMessage: "Please, confirm modal closing."
83+
},
84+
85+
/**
86+
* Added confirming for modal closing
87+
*
88+
* @returns {Element}
89+
*/
90+
closeModal: function () {
91+
if (!confirm(this.options.confirmMessage)) {
92+
return this.element;
93+
}
94+
95+
return this._super();
96+
}
97+
};
98+
99+
return function (targetWidget) {
100+
// Example how to extend a widget by mixin object
101+
$.widget('mage.modal', targetWidget, modalWidgetMixin); // the widget alias should be like for the target widget
102+
103+
return $.mage.modal; // the widget by parent alias should be returned
104+
};
105+
});
68106
```
69107

70108
## Declaring a mixin
@@ -76,7 +114,7 @@ The mixins configuration in the `requirejs-config.js` associates a target compon
76114

77115
### Example
78116

79-
The following is an example of a `requirejs-config.js` file that adds the `columns-mixins`, defined in the previous example, to the [grid column component].
117+
The following is an example of a `requirejs-config.js` file that adds the `columns-mixin` and `modal-widget-mixin` mixins, defined in the previous examples, to the [grid column component][] and [modal widget][].
80118

81119
**File:** `OrangeCompany/Sample/view/base/requirejs-config.js`
82120

@@ -86,6 +124,9 @@ var config = {
86124
mixins: {
87125
'Magento_Ui/js/grid/controls/columns': {
88126
'OrangeCompany_Sample/js/columns-mixin': true
127+
},
128+
"Magento_Ui/js/modal/modal": {
129+
"OrangeCompany_Sample/js/modal-widget-mixin": true
89130
}
90131
}
91132
}
@@ -114,4 +155,5 @@ The following is a list of files in the [`Magento_CheckoutAgreement`] module tha
114155
[`view/frontend/web/js/model/set-payment-information-mixin.js`]: {{ site.mage2bloburl }}/{{page.guide_version}}/app/code/Magento/CheckoutAgreements/view/frontend/web/js/model/set-payment-information-mixin.js
115156
[`Magento_CheckoutAgreement`]: {{ site.mage2bloburl }}/{{page.guide_version}}/app/code/Magento/CheckoutAgreements
116157
[About AMD modules and RequireJS]: {{ page.baseurl }}/javascript-dev-guide/javascript/js-resources.html#requirejs-library
158+
[modal widget]: {{ page.baseurl }}/javascript-dev-guide/widgets/widget_modal.html
117159
[Configure JS resources]: {{ page.baseurl }}/javascript-dev-guide/javascript/js-resources.html

0 commit comments

Comments
 (0)