Skip to content

Commit caa527f

Browse files
committed
new Template ajaxRestApi
1 parent 04603d4 commit caa527f

File tree

8 files changed

+173
-0
lines changed

8 files changed

+173
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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="${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}": {
11+
"waitLoadingContainer": "#${actionname}-loading"
12+
}
13+
}
14+
}
15+
</script>
16+
17+
<!-- Insert Js using data-mage-init element attribute -->
18+
data-mage-init='{
19+
"${actionname}": {
20+
"waitLoadingContainer": "#${actionname}-loading"
21+
}
22+
}'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This Template creates an Ajax action using custom rest API.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* ${ApiClassName}Interface
4+
*
5+
* @copyright Copyright © ${commentsYear} ${CommentsCompanyName}. All rights reserved.
6+
* @author ${commentsUserEmail}
7+
*/
8+
namespace ${Vendorname}\${Modulename}\Api;
9+
10+
11+
interface ${ApiClassName}Interface
12+
{
13+
/**
14+
* Description here...
15+
*
16+
* @return string
17+
*/
18+
public function ${actionname}();
19+
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* ${ApiClassName}
4+
*
5+
* @copyright Copyright © ${commentsYear} ${CommentsCompanyName}. All rights reserved.
6+
* @author ${commentsUserEmail}
7+
*/
8+
namespace ${Vendorname}\${Modulename}\Model;
9+
10+
use ${Vendorname}\${Modulename}\Api\${ApiClassName}Interface;
11+
12+
class ${ApiClassName} implements ${ApiClassName}Interface
13+
{
14+
public function ${actionname}()
15+
{
16+
// Do something
17+
$result = ['success' => 'Here comes the result'];
18+
19+
return json_encode($result);
20+
}
21+
}

templates/ajaxRestApi/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
2+
<preference for="${Vendorname}\${Modulename}\Api\${ApiClassName}Interface" type="${Vendorname}\${Modulename}\Model\${ApiClassName}"/>
3+
</config>

templates/ajaxRestApi/etc/webapi.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
2+
<route url="/V1/${modulename}/${actionname}" method="PUT">
3+
<service class="${Vendorname}\${Modulename}\Api\${ApiClassName}Interface" method="${actionname}"/>
4+
<resources>
5+
<resource ref="anonymous"/>
6+
</resources>
7+
</route>
8+
</routes>
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}: '${Vendorname}_${Modulename}/js/${actionname}'
11+
}
12+
}
13+
};
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
'mage/url',
12+
"jquery/ui",
13+
'mage/translate'
14+
], function ($, url) {
15+
"use strict";
16+
17+
$.widget('${vendorname}_${modulename}.${actionname}', {
18+
options: {
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+
url: url.build('/rest/V1/${modulename}/${actionname}'),
46+
data: this.dataForApiRequest(),
47+
cache: false,
48+
contentType: 'application/json',
49+
type: 'PUT',
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+
dataForApiRequest: function() {
59+
var data = {};
60+
// data[dataCode] = dataValue;
61+
return JSON.stringify(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+
var result = JSON.parse(response);
74+
if (result.success) {
75+
alert('Success: ' + result.success);
76+
}
77+
},
78+
79+
displayError: function (message) {
80+
alert(message);
81+
}
82+
});
83+
84+
return $.${vendorname}_${modulename}.${actionname};
85+
});

0 commit comments

Comments
 (0)