Skip to content

Commit 04603d4

Browse files
committed
New template frontAjaxJson
1 parent 506d41c commit 04603d4

File tree

6 files changed

+182
-0
lines changed

6 files changed

+182
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
1. Add the following code into your .phtml file where the ajax request will be triggered
2+
3+
<!-- loading request indicator -->
4+
<span id="ajax-${actionname}-loading" class="please-wait load indicator" style="display: none;"></span>
5+
6+
<!-- Insert Js using script tag -->
7+
<script type="text/x-magento-init">
8+
{
9+
"<element_selector>": {
10+
"${Actionname}Ajax": {
11+
"ajaxUrl": "<?php echo $block->getUrl('${vendorname}_${modulename}/ajax/${actionname}'); ?>",
12+
"waitLoadingContainer": "#ajax-${actionname}-loading"
13+
}
14+
}
15+
}
16+
</script>
17+
18+
<!-- Insert Js using data-mage-init element attribute -->
19+
data-mage-init='{
20+
"${Actionname}Ajax": {
21+
"ajaxUrl": "<?php echo $block->getUrl('${vendorname}_${modulename}/ajax/${actionname}'); ?>",
22+
"waitLoadingContainer": "#ajax-${actionname}-loading"
23+
}
24+
}'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This Template creates an Ajax action with Json return.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* ${Actionname}
4+
*
5+
* @copyright Copyright © ${commentsYear} ${CommentsCompanyName}. All rights reserved.
6+
* @author ${commentsUserEmail}
7+
*/
8+
9+
namespace ${Vendorname}\${Modulename}\Controller\Ajax;
10+
11+
use Magento\Framework\App\Action\Action;
12+
use Magento\Framework\App\Action\Context;
13+
14+
class ${Actionname} extends Action
15+
{
16+
/**
17+
* @param Context $context
18+
*/
19+
public function __construct(Context $context)
20+
{
21+
parent::__construct($context);
22+
}
23+
24+
/**
25+
* Ajax request
26+
*
27+
* @return void
28+
*/
29+
public function execute()
30+
{
31+
if ($this->getRequest()->isAjax()) {
32+
try {
33+
// Do something here
34+
$result = 'Return what you need here';
35+
$this->getResponse()->representJson(json_encode(['success' => $result, 'error' => '']));
36+
} catch (\Exception $e) {
37+
$this->getResponse()->representJson(json_encode(['success' => '', 'error' => $e->getMessage()]));
38+
}
39+
}
40+
}
41+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* routes
5+
*
6+
* @copyright Copyright © ${commentsYear} ${CommentsCompanyName}. All rights reserved.
7+
* @author ${commentsUserEmail}
8+
*/
9+
-->
10+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
11+
<router id="standard">
12+
<route id="${vendorname}_${modulename}" frontName="${vendorname}_${modulename}">
13+
<module name="${Vendorname}_${Modulename}"/>
14+
</route>
15+
</router>
16+
</config>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* require-config.js
3+
*
4+
* @copyright Copyright © ${commentsYear} ${CommentsCompanyName}. All rights reserved.
5+
* @author ${commentsUserEmail}
6+
*/
7+
var config = {
8+
map: {
9+
'*': {
10+
${Actionname}Ajax: '${Vendorname}_${Modulename}/js/ajax/${actionname}'
11+
}
12+
}
13+
};
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* ${actionname}
3+
*
4+
* @copyright Copyright © ${commentsYear} ${CommentsCompanyName}. All rights reserved.
5+
* @author ${commentsUserEmail}
6+
*/
7+
8+
/*jshint jquery:true*/
9+
define([
10+
"jquery",
11+
"jquery/ui",
12+
'mage/translate'
13+
], function ($) {
14+
"use strict";
15+
16+
$.widget('${vendorname}_${modulename}.${actionname}ajax', {
17+
options: {
18+
ajaxUrl: null,
19+
waitLoadingContainer: null
20+
},
21+
22+
/**
23+
* Bind a click handler on the widget's element.
24+
* @private
25+
*/
26+
_create: function() {
27+
this.element.on('click', $.proxy(this.clickAction, this));
28+
},
29+
30+
/**
31+
* Init object
32+
* @private
33+
*/
34+
_init: function () {
35+
// Do something if needed
36+
},
37+
38+
clickAction: function(event) {
39+
if ($(this.options.waitLoadingContainer).is(":visible")) {
40+
return false;
41+
}
42+
// Do something with element clicked $(event.target)
43+
44+
$.ajax({
45+
context: this,
46+
url: this.options.ajaxUrl,
47+
data: this.getDataForAjaxRequest(),
48+
cache: true,
49+
dataType: 'json',
50+
beforeSend: $.proxy(this.beforeSend, this),
51+
complete: $.proxy(this.complete, this),
52+
success: $.proxy(this.success, this),
53+
error: $.proxy(this.displayError, this, $.mage.__('Sorry, something went wrong.')),
54+
});
55+
56+
},
57+
58+
getDataForAjaxRequest: function() {
59+
var data = {};
60+
// data[dataCode] = dataValue;
61+
return data;
62+
},
63+
64+
beforeSend: function () {
65+
$(this.options.waitLoadingContainer).show();
66+
},
67+
68+
complete: function () {
69+
$(this.options.waitLoadingContainer).hide();
70+
},
71+
72+
success: function (response) {
73+
if (response.success) {
74+
alert('Success: ' + response.success);
75+
}
76+
if (response.error) {
77+
alert('Error: ' + response.error);
78+
}
79+
},
80+
81+
displayError: function (message) {
82+
alert(message);
83+
}
84+
});
85+
86+
return $.${vendorname}_${modulename}.${actionname}ajax;
87+
});

0 commit comments

Comments
 (0)