在prestashop的页脚中显示我的模块JS [英] Show my module JS at footer in prestashop

查看:112
本文介绍了在prestashop的页脚中显示我的模块JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在prestashop中开发一个模块.我的模块有一个JS配置页面.我正在使用displayBackOfficeHeader挂钩在标头中添加我的JS.但是在安装模块后,当我配置模块时,这会给我Jquery问题,因为顶部添加的JS意味着在jquery.js

I am developing a module in prestashop. I have a JS for configuration page for my module. I am using displayBackOfficeHeader hook to add my JS in header. But after installing my module when i configure my module it's give me Jquery issue because my JS in adding at top means before jquery.js

问题1)如何管理我的JS应该在Jquery.js之后添加标头?

Que 1) How to manage that my JS should add in header after Jquery.js?

问题2)如果我们不能与que Ist一样管理,那么如何在页脚中添加JS?

Que 2) If we can't manage same as que Ist then how to add JS in footer?

推荐答案

在大多数情况下要将任何资产(JavaScript或CSS)添加到后台(管理页面),您应该使用钩子actionAdminControllerSetMedia(). 正确注册JavaScript文件的完整步骤是:

In most cases to add any asset (JavaScript or CSS) to a back-office (admin pages) you should use the hook actionAdminControllerSetMedia(). Full steps to register correctly a JavaScript file are:

步骤1.在模块安装上注册钩子:

Step 1. Register the hook on a module install:

public function install()
{
    if (!parent::install()) {
        return false;
    }

    // After a module installation, register the hook
    if (!$this->registerHook('actionAdminControllerSetMedia')) {
        return false;
    }

    return true;
}

第2步.然后,添加您的JavaScript资产:

Step 2. Then, add your JavaScript asset:

public function hookActionAdminControllerSetMedia()
{
    // Adds jQuery and some it's dependencies for PrestaShop
    $this->context->controller->addJquery();

    // Adds your's JavaScript from a module's directory
    $this->context->controller->addJS($this->_path . 'views/js/example.js');
}

可以使用多种方法和几种方法在后台(管理页面)中注册资产(按执行顺序列出):

There are different ways and several methods that can be used to register assets in a back-office (admin pages) (they are listed in the order of the execution):

  1. 挂钩hookDisplayBackOfficeHeader()
  2. 控制器的方法AdminControllerCore::setMedia()
  3. 挂钩actionAdminControllerSetMedia()
  4. 模块的方法Module::getContent()
  5. 挂钩hookDisplayBackOfficeFooter()
  1. The hook hookDisplayBackOfficeHeader()
  2. The controller's method AdminControllerCore::setMedia()
  3. The hook actionAdminControllerSetMedia()
  4. A module's method Module::getContent()
  5. The hook hookDisplayBackOfficeFooter()

要添加内联代码,最好的方法是使用钩子hookDisplayBackOfficeFooter().例如:

To add an inline code, the best way is use the hook hookDisplayBackOfficeFooter(). For example:

public function hookDisplayBackOfficeFooter()
{
    return '
        <script type="text/javascript">
            var EXAMPLE_VARIABLE = "Hello, Zapalm!";
        </script>
    ';
}

再举一个例子,说明需要添加JavaScript资产但在模块的AdminController的子类中的情况(对于PrestaShop 1.7):

public function setMedia($isNewTheme = false)
{
    parent::setMedia($isNewTheme);

    $this->addCSS(_MODULE_DIR_ . $this->module->name . '/views/css/example.css');
    $this->addJS(_MODULE_DIR_ . $this->module->name . '/views/js/example.js');
}

对于PrestaShop 1.6和1.5,您也可以像本例一样进行操作,但是您需要从方法定义和父方法调用中删除$isNewTheme参数.

For PrestaShop 1.6 and 1.5 you can do also like in this example but you need to remove $isNewTheme parameter from the method definition and the parent method call.

参考:

  1. PrestaShop 1.7中的资产管理.

如何在PrestaShop 1.7中将外部资产添加到前台页面.

使用哪种方法的说明:addJS或registerJavascript.

这篇关于在prestashop的页脚中显示我的模块JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆