Skip to content

Commit f1505bf

Browse files
committed
[Task] Use magento modal alert to display ajax errors
1 parent 17d2055 commit f1505bf

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

templates/ajaxHtml/view/frontend/web/js/ajax/${actionname}.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
/*jshint jquery:true*/
99
define([
1010
"jquery",
11-
"jquery/ui",
11+
'Magento_Ui/js/modal/alert',
1212
'mage/translate'
13-
], function ($) {
13+
], function ($, alert) {
1414
"use strict";
1515

1616
$.widget('${vendorname}_${modulename}.${actionname}ajax', {
@@ -75,7 +75,13 @@ define([
7575
},
7676

7777
displayError: function (message) {
78-
alert(message);
78+
alert({
79+
title: $.mage.__('An error occurred'),
80+
content: message,
81+
actions: {
82+
always: function(){}
83+
}
84+
});
7985
}
8086
});
8187

templates/ajaxJson/view/frontend/web/js/ajax/${actionname}.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
/*jshint jquery:true*/
99
define([
1010
"jquery",
11-
"jquery/ui",
11+
'Magento_Ui/js/modal/alert',
1212
'mage/translate'
13-
], function ($) {
13+
], function ($, alert) {
1414
"use strict";
1515

1616
$.widget('${vendorname}_${modulename}.${actionname}ajax', {
@@ -71,15 +71,22 @@ define([
7171

7272
success: function (response) {
7373
if (response.success) {
74+
// Do something with response.success json
7475
alert('Success: ' + response.success);
7576
}
7677
if (response.error) {
77-
alert('Error: ' + response.error);
78+
this.displayError('Error: ' + response.error);
7879
}
7980
},
8081

8182
displayError: function (message) {
82-
alert(message);
83+
alert({
84+
title: $.mage.__('An error occurred'),
85+
content: message,
86+
actions: {
87+
always: function(){}
88+
}
89+
});
8390
}
8491
});
8592

templates/ajaxRestApi/view/frontend/web/js/${actionname}.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
define([
1010
"jquery",
1111
'mage/url',
12-
"jquery/ui",
12+
'Magento_Ui/js/modal/alert',
1313
'mage/translate'
14-
], function ($, url) {
14+
], function ($, url, alert) {
1515
"use strict";
1616

1717
$.widget('${vendorname}_${modulename}.${actionname}', {
@@ -72,12 +72,19 @@ define([
7272
success: function (response) {
7373
var result = JSON.parse(response);
7474
if (result.success) {
75+
// Do something with result.success api response
7576
alert('Success: ' + result.success);
7677
}
7778
},
7879

7980
displayError: function (message) {
80-
alert(message);
81+
alert({
82+
title: $.mage.__('An error occurred'),
83+
content: message,
84+
actions: {
85+
always: function(){}
86+
}
87+
});
8188
}
8289
});
8390

0 commit comments

Comments
 (0)