Jump to content

setMedia() addJS not loading javascript files


Recommended Posts

Hi, when I am trying to load js in a ModuleFrontController through the setMedia function it does not seem to pull through. This also happens when I put the css in a sub folder, but works when it is in the top folder.

 

/**
* Set default medias for this controller
*/
public function setMedia(){
parent::setMedia();

$this->context->controller->addCSS(array(
$this->module->getLocalPath() . 'css/customcase.css',
));

$this->context->controller->addJS(array(
$this->module->getLocalPath() . 'js/customcase.js',
));
}

 

Putting the js in the top directory does not seem wot work either

 

/**
* Set default medias for this controller
*/
public function setMedia(){
parent::setMedia();

$this->context->controller->addCSS(array(
$this->module->getLocalPath() . 'customcase.css',
));

$this->context->controller->addJS(array(
$this->module->getLocalPath() . 'customcase.js',
));
}

 

And help would be appreciated if you can see what I am doing wrong.

 

Thanks

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

Shoulders,

 

I understand Tools::addJS() is deprecated in version 1.5 and should not be used anymore.

 

Dawb,

 

I have the same problem as you... when using setMedia() in my ModuleFrontController it doesn't add the Javascript with addJS(). Without bein sure, I think the problem will be the same with addCSS().

 

I have tried to put the JS and CSS files in the <moduleDIR>/views/js and <moduleDIR>/views/css as indicated in some documents of the developper guide, even putting it in the <moduleDIR>/js and <moduleDIR>/css doesn't work either.

 

Although adding the hard coded Javascript call in the template file is working, I would prefer to use the recommended method via $this->context->controller->addJS().

 

Any idea someone why it's not working for the ModuleFrontController?

 

Thanks!

 

BR,

RaPhiuS

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

I have tried replacing directly with "$this->module->getLocalPath()" instead of the "_MODULE_DIR_.$this->module->name" to make it less dependant on the global variable....

 

When I output the $this->module->getLocalPath() with XAMPP I get: "C:\xampp\htdocs\prestashop/modules/siscore/". I think the problem is with the \ character on Windows, therefore the file can't be located... hence failing...

 

I guess this will only be the case when using Windows and should not happen on any host systems using regular Unix or Linux operating systems... but i might be wrong here....

 

Any thoughts?

 

Br,

RaPhiuS

Link to comment
Share on other sites

Shoulders,

 

I understand Tools::addJS() is deprecated in version 1.5 and should not be used anymore.

 

 

cheers for this, it will help me make changes to my skeleton blank module for 1.5, but where did you get this information form so i can read up on this and other things. I want to make my module compliant. Sorry this is a little off topic.

Link to comment
Share on other sites

To add on this:

 

Since from the context member you have access to the controller member, you therefore have access to the methods exposed by the Controller object.

 

Since ModuleFrontController inherits from all that, you have the direct access to addJS() and addCSS().

 

Hopes that clarifies!

 

BR,

RaPhiuS

Link to comment
Share on other sites

thanks for the heads up. i am going to have a read. one trick i have just discovered is that if you have debug on it tells you that these methods (Tools::addJS) are depreceated. I dont suppose you know where in prestashop these errors/checking routines are stored because then i can just go and read them to check if i am using the old versions of stuff.

 

I am just getting into OOP, its fun.

Link to comment
Share on other sites

it might be the format on how you declare the location off the files.

 

css/myfile.css will load the css file in the directory 'css' down from where the script is running.

/css/myfile.css will run the file in the 'css' folder down from the root of your public web

also try ./css/myfile.css

 

it sounds like the suntax of the path is wrong. this is the bvest i can do because i have not quite got on to controllers yet.

Link to comment
Share on other sites

Hello Shoulders,

 

Thanks for your feedback. The more I look into this, the more I think this is related to Windows use of \ instead of regular / available on all Unix or Linux systems.

 

In the meantime, if someone knows, idea are still welcome. For the time being, I have fixed everything that I wanted, both JS and CSS and I will be happy to use "_MODULE_DIR_.$this->module->name" even if the call to _MODULE_DIR_ is not something that I would like since it is a global variable...

 

I will try $this->module->getLocalPath() when I get into testing my module on a real host and I don't know why, I am pretty sure that it will work...

 

Controllers are fun and very intuitive, Prestashop is a great platform exposing a well polished API. I hope this topic can be of use for some other people out there who might run into the same problems.

 

Cheers,

RaPhiuS

Link to comment
Share on other sites

hi mate i did not see that you are running on windows. in windows, xampp you must use

c:\mywebsite\css\my.css

etc. sometime you have to double the slashes ie

public $log_path = 'D:\\joomla\\www\\htdocs\\logs';

 

also, you can use phpinfo() to get the path of the location from where you are running the script.

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

×
×
  • Create New...