Jump to content

AJAX module or similar approach


Jonathan Rodz

Recommended Posts

Hello everyone,

 

I'm getting crazy googling and trying to understand the ajax functionality on modules. I really hope I can find some knowledge to bring some light in, cos it's beeing looooong hours already.

 

Scenario:

 

I create a module on homepage showing some feature product. All great. I'm quering the DB, I got my results, I show them, and all is perfect, but I've been asked to change that "feature list" based on different criteria (diferent queries).

 

I'm thinking on different possibilities:

 

1.- Reload the hook passing a different parameter (I think I can't reload a module without reloading the page).

2.- Access to the controller from .tpl file (user interaction) on real time to re-display with different values (diferent functions reloading the same tpl with different values... maybe).

3.- Load content straigh with AJAX, ignoring mvc (I'm not fan of that idea, but I didn't achive that solution either because DB access outside the framework).

4.- Create a few different modules and switch them (but if I load all of them at once the page will load really slow, so it has to be ajax somehow).

 

I didn't copy the example cos it's a little bit complex, but I'll be happy to share my code once I know what to share without flooding the topic.

 

Any inspiration it will be appreciated

 

Thanks in advance

Edited by Jonathan Rodz (see edit history)
Link to comment
Share on other sites

Hi Jonathan,

 

If you have a controller for your module, ajax in POST mode have to call your controller.

 

Your controller will not display tpl because you will add 'ajax=true' for example and then your code will only deal with json output for example and not display the controller html part.

 

Ajax calling in POST mode, transmit also Cookies, you will then, in your controller, have access to guest id, country id...

 

Best regards

Edited by math_php (see edit history)
  • Like 1
Link to comment
Share on other sites

Hi Jonathan,

 

Let type to 'get' for debug and dev time and further if and only if you do not need info from the framework :

 

<script type="text/javascript"> function QuickLook() { $.ajax({ url: '{$base_dir}modules/quicklook/quicklook-ajax.php', type: 'get',

 

On stackoverflow I do not see  'quicklook-ajax.php' ?

Is it a real file or do you think prestashop is going to call your method 'ajaxCall' ? Prestashop will not.

 

I think you are really close, but I or you are misunderstanding something :)

  • Like 1
Link to comment
Share on other sites

thanks math_php for you indications!! :) :) :)

 

at the end all is solved with the bridge file

 

first I call it (bridge file) through AJAX:

 

$.ajax({
      type: 'POST',
      url: baseDir + 'modules/homefeatured/homefeatured-ajax.php',
      data: 'ajax=true',
      dataType: 'html',
      success: function(data) {
        $('#test').html(data);
      }
});

 

that's the bridge:

 

<?php

include(dirname(__FILE__).'/../../config/config.inc.php');
include(dirname(__FILE__).'/../../init.php');
include(dirname(__FILE__).'/homefeatured.php');

$home = new HomeFeatured();
echo $home->ajaxCall();

?>

 

and I created on homefeatured.php this ajaxCall function:

 

public function ajaxCall() {

     global $smarty;

    //all you wanna process, within the framework

}

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...