Jump to content

[SOLVED] Calling javascript from custom module


feho1
 Share

Recommended Posts

Hi, following the official guide how to create module i have created my custom module and now i want to call JavaScript function or file to execute function in my module using hooks,in my from custom module from hookActionCustomerAccountUpdate or any other hook i tried but unsuccessful , I have registered the hooks and 
$this->context->controller->registerJavascript( // 'my-module', // 'modules/' . $this->name . '/views/js/my-module.js', // array( // 'position' => 'bottom', // 'priority' => 2 // ) // );

$this->context->controller->registerJavascript(
			'my-module',
				'modules/' . $this->name . '/views/js/my-module.js',
			array(
					'position' => 'bottom',
				'priority' => 2
				)
		);

and when i enter this into hookActionCustomerAccountUpdate it does not call the javascript which is alert, but in header it always calls in every page understandably.

What I have tried: 
calling header hook from hookActionCustomerAccountUpdate Hook::exec, with this->hookHeader.



My question is how do i call javascript or javascript function or Ajax on hook in module ?

Edited by feho1 (see edit history)

Share this post


Link to post
Share on other sites

27 minutes ago, musicmaster said:

Do you have the optimization setting that javascript is moved to the bottom enabled? Often that causes this kind of problems.

I'm not sure what you mean. 

In the back office or in module php files? 

Share this post


Link to post
Share on other sites

3 hours ago, musicmaster said:

backoffice

Yes found it Smart cache for JavaScript, it is disabled, should i enable ? 
Or can you show me an example how to call javascript or ajax function from module inside action hook ?

Edited by feho1 (see edit history)

Share this post


Link to post
Share on other sites

3 minutes ago, musicmaster said:

No, you shouldn't disable that.

Moving javascript to the bottom was an option in PS 1.6. In 1.7 you would need some extension for it.

I dont have the move javascript to the bottom i have only " Smart cache for JavaScript" and enabled it, but still cant call from module a javascript function or Ajax. 
i have looked other modules but dont get how to call ajax or javascript, I just want to call ajax function after user updated information with hookActionCustomerAccountUpdate  hook.

Share this post


Link to post
Share on other sites

check if hook  "actionCustomerAccountUpdate" is correctly registered in your 'my-module'

SELECT m.*, hm.*, h.name as hook 
  FROM `ps_module` m 
  JOIN ps_hook_module hm ON hm.id_module = m.id_module 
  JOIN ps_hook h on h.id_hook = hm.id_hook 
WHERE m.name = 'my-module' 

 

  • Thanks 1

Share this post


Link to post
Share on other sites

15 hours ago, EvaF said:

check if hook  "actionCustomerAccountUpdate" is correctly registered in your 'my-module'

SELECT m.*, hm.*, h.name as hook 
  FROM `ps_module` m 
  JOIN ps_hook_module hm ON hm.id_module = m.id_module 
  JOIN ps_hook h on h.id_hook = hm.id_hook 
WHERE m.name = 'my-module' 

 

Yes it is registered:
image.png.e18bc2aa97d383dc89154f54200b2cc7.png

Share this post


Link to post
Share on other sites

ok, I have checked the  Hook::exec of ActionCustomerAccountUpdate and it seems to me, that your javascript cannot be placed because of redirect.
(controllers/front/IdentityController.php  line 55)

 

// within submit is processed save -> 
// within save is processed update -> 
// within successfull update is executed hook actionCustomerAccountUpdate
        if ($customer_form->submit()) {
                $this->success[] = $this->trans('Information successfully updated.', array(), 'Shop.Notifications.Success');
                $should_redirect = true;
            } else {
 				
                $this->errors[] = ...
            }
        }
        ...
        if ($should_redirect) {
            $this->redirectWithNotifications($this->getCurrentURL());
        }


 

  • Thanks 1

Share this post


Link to post
Share on other sites

36 minutes ago, EvaF said:

ok, I have checked the  Hook::exec of ActionCustomerAccountUpdate and it seems to me, that your javascript cannot be placed because of redirect.
(controllers/front/IdentityController.php  line 55)

 

// within submit is processed save -> 
// within save is processed update -> 
// within successfull update is executed hook actionCustomerAccountUpdate
        if ($customer_form->submit()) {
                $this->success[] = $this->trans('Information successfully updated.', array(), 'Shop.Notifications.Success');
                $should_redirect = true;
            } else {
 				
                $this->errors[] = ...
            }
        }
        ...
        if ($should_redirect) {
            $this->redirectWithNotifications($this->getCurrentURL());
        }


 

Thank you for the info, should i override it ? is that possible ?

Share this post


Link to post
Share on other sites

I do not know where (at which page - my-account, or identity or..) do you want to load your javascript file.
to be true, when I am reading list of your hooks, I would do it by simply way: ( there are certainly more ellegant way)

in hookActionCustomerAccountUpdate i would set the cookie value f.e.
 

        $this->context->cookie->customer_account_update = true;

and in hookHeader I would check if cookie "customer_account_update" is set

if (isset($this->context->cookie->customer_account_update)){
  $this->context->controller->registerJavascript(
     'my-module',
     'modules/' . $this->name . '/views/js/my-module.js',
      array(
        'position' => 'bottom',
        'priority' => 2
	  )
   );
   unset($this->context->cookie->customer_account_update);
}

P.S. I wrote it blindly - didn't test it

Edited by EvaF (see edit history)
  • Thanks 1

Share this post


Link to post
Share on other sites

7 hours ago, EvaF said:

I do not know where (at which page - my-account, or identity or..) do you want to load your javascript file.
to be true, when I am reading list of your hooks, I would do it by simply way: ( there are certainly more ellegant way)

in hookActionCustomerAccountUpdate i would set the cookie value f.e.
 

        $this->context->cookie->customer_account_update = true;

and in hookHeader I would check if cookie "customer_account_update" is set

if (isset($this->context->cookie->customer_account_update)){
  $this->context->controller->registerJavascript(
     'my-module',
     'modules/' . $this->name . '/views/js/my-module.js',
      array(
        'position' => 'bottom',
        'priority' => 2
	  )
   );
   unset($this->context->cookie->customer_account_update);
}

P.S. I wrote it blindly - didn't test it

Thank you very much. your code works flawlessly :) Marking as solved .
Thanks again for your time and sharing your knowledge.. 
And Thanks to musicmaster too :)

Share this post


Link to post
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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More