I've created a controller in Magento which check whether or not there are products in a list. If there are products in list it will return true
otherwise false
.
Here is the front-end which triggers the ajax call, bare in mind I can not change this to be a form. It has to be a link.
<a href="[my_controller]" title="Compare Products" target="_blank" class="compare-product-link">Compare Products</a>
Here is the ajax call.
jQuery(".compare-product-link").on("click", function(e) {
jQuery.ajax({
async : false,
dataType : "json",
url : "/compareextra/compare/allowed",
success : function(data) {
//console.log(data);
if(data.isAllowed != true) {
e.preventDefault();
}
}
});
});
The problem I have is that the async
is deprecated and is not good for user experience, saying that there are many answer out there which add a delay of 3 seconds, I also don't want that because thats not good for user experience.
I've also tried using a promise call but it only works with async : false
.
jQuery(".compare-product-link").on("click", function(e) {
var response = false;
jQuery.ajax({
dataType : "json",
url : "/compareextra/compare/allowed",
success : function(data) {
console.log(data);
if(data.isAllowed) {
response = true;
}
}
}).done(function (){
console.log(response);
if(response != true) {
e.preventDefault();
}
});
});
EDIT
Another problem I also have is if I store the link into a variable and then open a new window as so window.location = href;
most browser will block it and users will have to manually accept pop ups from the target site, which again is not good for user experience.